As seen on the previous tutorial pages, ereg( ) and eregi( ) are used to find patterns. Taking this to the next level will allow you to find a pattern and replace it with a new value. This is using ereg_replace( ) and eregi_replace( ). The i has the same effect as before, case insensitive.
The replace command looks like this :
Very similar to the normal eregi command only it has an extra parameter in the middle.
A replacement can be used for many different uses such as...
1. Taking an entered URL and making it a clickable one.
2. Replacing a text smilie into an image one.
3. Replacing bad words with *@!%.
Most of these examples would be taking some user input from a form and processing it to a webpage or email. We'll do an example using the smilies.
On the form page
changeit.php
Here is the test in action. If you enter :) into the text box, it will appear as a smilie image in the second cell. The current page will reload upon submit, so you will have to scroll down to see the result here.
The replace command looks like this :
eregi_replace(pattern,replacement,search_area);
Very similar to the normal eregi command only it has an extra parameter in the middle.
A replacement can be used for many different uses such as...
1. Taking an entered URL and making it a clickable one.
2. Replacing a text smilie into an image one.
3. Replacing bad words with *@!%.
Most of these examples would be taking some user input from a form and processing it to a webpage or email. We'll do an example using the smilies.
On the form page
<form method="post" action="changeit.php">
<input type="text" name="entry">
<input type="submit" value="submit">
</form>
<input type="text" name="entry">
<input type="submit" value="submit">
</form>
changeit.php
<?php
$pattern = ":\)";
$replacement = "<img src=\"smilie.gif\">";
$entry = ereg_replace($pattern,$replacement,$entry);
echo "$entry";
?>
$pattern = ":\)";
$replacement = "<img src=\"smilie.gif\">";
$entry = ereg_replace($pattern,$replacement,$entry);
echo "$entry";
?>
Here is the test in action. If you enter :) into the text box, it will appear as a smilie image in the second cell. The current page will reload upon submit, so you will have to scroll down to see the result here.

