iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (35)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

August 8, 2008
Reader Question: What graphics compression program do you use?
About
 
August 7, 2008
Google's Big Mistake: Getting Rid of Google Page Creator, What Do...
About
 
August 7, 2008
Wish XML a happy birthday
About
 
August 7, 2008
Poll: How important is SEO to your overal website strategy?
About
 
August 7, 2008
How to Create a Search Feature with PHP and MySQL
WebReference.com
 
August 7, 2008
1 comment
.net
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP /Introduction to PHP

Replace Functions 

  Views:    2871
  Votes:    1
by John Doe 12/12/03 Rating: 

Synopsis:

This article teach how to make some text replacements using simple PHP functions.
Pages: 
The Article

This article teach how to make some text replacements using simple PHP functions. Let's get started.

Lesson 1: Simple replace

 
<?php
// This is the text we will use for make replacements
$text = "Today is sunday!";

// The word that we will replace
$word = "sunday";

// The replacement
$replacement = "saturday";

// Now, we use the function ereg_replace to make the replacement
$newText = ereg_replace($word, $replacement, $text);

echo $newText;
?>

 

this will return "Today is saturday!"

Very simple no? Now you can see how useful it is:

Lesson 2: Making a smiles system

We can use the function ereg_replace to replace simple characters like ":)" and ":(" in images. See:

<?php
// The text with the smiles
$text = "I'm happy: :). I'm sad: :(.";

// Define the smiles and the images
$s[1] = ":\)"; $i[1] = "<img src=happy.gif>";
$s[2] = ":\("; $i[2] = "<img src=sad.gif>";

// Now, just replace!
for($k = 1; $k <= count($s); $k++) {
    $text = ereg_replace($s[$k], $i[$k], $text);
}

echo $text;
?>


If you have set the correct image names (for the smiles) you will not see the characters ":)" and ":(", instead of this you will see the smiles images!

That's it. You have the example, and it's very easy to make a lot of different things. Just keep trying.

Useful links: www.php.net
                 www.php.net/regex

Pages: 



 
  Sponsors