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