Notice: Undefined index: testing in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/webdesign/Programming_Advanced/regularexpressions_php.html on line 127

Notice: Undefined variable: docRoot_secure in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/webdesign/Programming_Advanced/regularexpressions_php.html on line 143

Training

"Winners make choices,
losers make excuses.
"
Decide to be a Winner!!!!

±Arrows Getting Started

± Phase 1
Planning

± Website Tools

± Phase 3
Conceptual Design

± Helpful Information

± Phase 4
Physical Design

± Phase 5
Testing

± Phase 6
Implement and Market Website

± Other Web Development Items

± Multimedia

± Useful Utilities

± Programming

± Advanced Programming

± Microsoft Office Products

± Computer Maintenance

± Other


Web Design

NOTE: This is a collection of information and links collected over the years that might provide useful information. A Safer Company LLC does not guarantee, endorse or approve any of these links or their scripts. Use these links to other websites at your own risk.


Notice: Undefined variable: message in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/webdesign/Templates/code/dir_listing_enhanced.php on line 67

Notice: Undefined variable: message in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/webdesign/Templates/code/dir_listing_enhanced.php on line 72

Warning: Use of undefined constant r - assumed 'r' (this will throw an Error in a future version of PHP) in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/code/F_get_page_title_Enh.php on line 28

Notice: Undefined variable: print_file_data in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/webdesign/Templates/code/dir_listing_enhanced.php on line 127

Warning: Use of undefined constant r - assumed 'r' (this will throw an Error in a future version of PHP) in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/code/F_get_page_title_Enh.php on line 28

Warning: Use of undefined constant r - assumed 'r' (this will throw an Error in a future version of PHP) in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/code/F_get_page_title_Enh.php on line 28

Warning: Use of undefined constant r - assumed 'r' (this will throw an Error in a future version of PHP) in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/code/F_get_page_title_Enh.php on line 28

Warning: Use of undefined constant r - assumed 'r' (this will throw an Error in a future version of PHP) in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/code/F_get_page_title_Enh.php on line 28

Warning: Use of undefined constant r - assumed 'r' (this will throw an Error in a future version of PHP) in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/code/F_get_page_title_Enh.php on line 28

Warning: Use of undefined constant r - assumed 'r' (this will throw an Error in a future version of PHP) in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/code/F_get_page_title_Enh.php on line 28

Warning: Use of undefined constant r - assumed 'r' (this will throw an Error in a future version of PHP) in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/code/F_get_page_title_Enh.php on line 28

Warning: Use of undefined constant r - assumed 'r' (this will throw an Error in a future version of PHP) in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/code/F_get_page_title_Enh.php on line 28

Warning: Use of undefined constant r - assumed 'r' (this will throw an Error in a future version of PHP) in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/code/F_get_page_title_Enh.php on line 28

Warning: Use of undefined constant r - assumed 'r' (this will throw an Error in a future version of PHP) in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/code/F_get_page_title_Enh.php on line 28

Warning: Use of undefined constant r - assumed 'r' (this will throw an Error in a future version of PHP) in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/code/F_get_page_title_Enh.php on line 28

Warning: Use of undefined constant r - assumed 'r' (this will throw an Error in a future version of PHP) in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/code/F_get_page_title_Enh.php on line 28

Warning: Use of undefined constant r - assumed 'r' (this will throw an Error in a future version of PHP) in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/code/F_get_page_title_Enh.php on line 28

Warning: Use of undefined constant r - assumed 'r' (this will throw an Error in a future version of PHP) in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/code/F_get_page_title_Enh.php on line 28

Notice: Undefined variable: print_folder_data in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/webdesign/Templates/code/dir_listing_enhanced.php on line 160

Notice: Undefined variable: message in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/webdesign/Templates/code/WebDesign_toc.php on line 12

Notice: Undefined variable: message in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/webdesign/Programming_Advanced/regularexpressions_php.html on line 251

Preg_Replace PHP Function

The preg_replace function is used to find and replace on a string or an array. We can give it one thing to find and replace or we can give it a full list of things (an array) to search for, each with a corresponding replacement.

preg_replace ( $search_for, $replace_with, $string , optional_limit, optional_count )

  • limit - will default to -1 which is no limit.
  • $string can be a string or an array.

Example 1 - Replacing one phrase:

<?
	//Replace a single word - Replace the word "the" with the word "a"
	$sting = "There are many examples to show the pred_replace function.";
	echo "$sting <br />";

	 $find ="/the/i";  //The i means to ignore case (upper or lower)
	 $replace ="a";
 
 	echo preg_replace ($find, $replace, $sting); 
?>
"; $find ="/the/i"; //The i makes this case insensitive $replace ="a"; echo preg_replace ($find, $replace, $sting); ?>

In the above example: "There are many examples to show the pred_replace function."

  • The first "The" is changed to a
  • The second "the" is changed to a
  • Giving the sentence "are are many examples to show a pred_replace function."

Example 2 - Using an Array:

<?
  $string = "The cat likes to sit on the fence. He also likes to climb the tree."; 
  //create arrays 
  $find_array = array ('/the/', '/cat/'); 
  $replace_array = array ('a', 'dog'); 
  //Replace with array values 
  echo preg_replace ($find_array, $replace_array, $string);
?>

In the example above:

  • the word "the" was replaced with the word "a"
  • the word "cat" was replaced with the word "dog"
  • Giving the sentence "The dog likes to sit on a fence. He also likes to climb a tree."

Example 3 - Replace ONCE

<?php 
 //Replace just once 
 echo preg_replace ($find_array, $replace_array, $string, 1);
 ?>

Notice: Undefined variable: find_array in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/webdesign/Programming_Advanced/regularexpressions_php.html on line 333

Notice: Undefined variable: replace_array in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/webdesign/Programming_Advanced/regularexpressions_php.html on line 333

Notice: Undefined variable: string in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/webdesign/Programming_Advanced/regularexpressions_php.html on line 333

Warning: preg_replace(): Empty regular expression in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/webdesign/Programming_Advanced/regularexpressions_php.html on line 333

The example above:

  • the word "the" was replaced with the word "a" ONLY ONCE
  • the word "cat" was replaced with the word "dog" ONLY ONCE
  • Giving the sentence "The dog likes to sit on a fence. He also likes to climb the tree." NOTICE that the second "the" was not replaced

Example - Count Replacements

<?php 
 //Keep a count of replacements 
 $count = 0; 
 echo preg_replace ($find_array, $replace_array, $string, -1, $count); 
 echo "<p>You have made $count replacements</p>"; 
 ?>

Notice: Undefined variable: find_array in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/webdesign/Programming_Advanced/regularexpressions_php.html on line 357

Notice: Undefined variable: replace_array in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/webdesign/Programming_Advanced/regularexpressions_php.html on line 357

Notice: Undefined variable: string in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/webdesign/Programming_Advanced/regularexpressions_php.html on line 357

Warning: preg_replace(): Empty regular expression in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/webdesign/Programming_Advanced/regularexpressions_php.html on line 357

You have made 0 replacements

In the example above:

  • the word "the" was replaced with the word "a"
  • the word "cat" was replaced with the word "dog"
  • Giving the sentence "The dog likes to sit on a fence. He also likes to climb a tree."
  • The difference is that the $count of the number of replacements

top of page


Warning: Use of undefined constant PHP_SELF - assumed 'PHP_SELF' (this will throw an Error in a future version of PHP) in /hermes/walnacweb03/walnacweb03af/b1896/as.asaferco/webdesign/Programming_Advanced/regularexpressions_php.html on line 375

Page last updated: May 31, 2012 14:46 PM

It is all about:
Content and Navigation...

Web Site by: A Safer Company LLC