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.

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);
 ?>

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>"; 
 ?>

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

Page last updated: May 31, 2012 10:46 AM

It is all about:
Content and Navigation...

Web Site by: A Safer Company LLC