View Full Version : ereg_replace, doesnt work for all smileys?

07-29-2008, 01:22 AM
My Problem is Solved! All I needed to do was escape the ( charecter for some reason...

Hi, i was just playing around with php (its how you learn :P)
and I came up with this script:

$image = "http://ludatha.com/.smileys/";

$content = $_POST['content'];
$content = ereg_replace(":)", "<img src='" . $image . "smiley1.gif' alt='' />", $content);
$content = ereg_replace(";)", "<img src='" . $image . "smiley2.gif' alt='' />", $content);
$content = ereg_replace(":O", "<img src='" . $image . "smiley3.gif' alt='' />", $content);
$content = ereg_replace(":o", "<img src='" . $image . "smiley3.gif' alt='' />", $content);
$content = ereg_replace(":0", "<img src='" . $image . "smiley3.gif' alt='' />", $content);
$content = ereg_replace(":D", "<img src='" . $image . "smiley4.gif' alt='' />", $content);
$content = ereg_replace(":d", "<img src='" . $image . "smiley4.gif' alt='' />", $content);
$content = ereg_replace(":S", "<img src='" . $image . "smiley5.gif' alt='' />", $content);
$content = ereg_replace(":s", "<img src='" . $image . "smiley5.gif' alt='' />", $content);
$content = ereg_replace(":(", "<img src='" . $image . "smiley6.gif' alt='' />", $content); // Line 15

$content = ereg_replace("/n", "<br />", $content);

echo $content;
echo "<br /><br />";
echo '<a href="ellie.php">Go Back</a>';
echo '<form id="form1" name="form1" method="post" action="ellie.php">
<textarea name="content" id="content" cols="45" rows="5"></textarea><br /><input name="submit" type="submit" value="Go!" />


Now its simple and unsecure I know, but it doesnt work when you run it.
It says there is an error on line 15, If you comment out line 15 the script runs...

Why doesnt :( work, Help a newbie :)

$content = ereg_replace(":(", "<img src='" . $image . "smiley6.gif' alt='' />", $content); // Line 15

I dont see any difference bewteen that and other lines above it...

The error is:

Warning: ereg_replace() [function.ereg-replace]: REG_EPAREN in D:\wamp\www\ellie.php on line 17

07-29-2008, 04:14 AM
What is the error?

07-29-2008, 12:44 PM
Warning: ereg_replace() [function.ereg-replace]: REG_EPAREN in D:\wamp\www\ellie.php on line 15

I should have posted that sorry :P

EDIT: I solved the problem, I had to escape the ( charecter for some reason...

07-29-2008, 10:47 PM
For some reason?

It's because it is used for grouping characters together.

Why are you still using the ereg functions anyhow? It's going down like a flaming oil tanker with the advent of PHP6. ;) That and the PCRE functions (preg_*) are much more efficient.

Besides, with that code the use of ereg_replace() is worthless. You're not matching any patterns, leaving with needless overhead. str_replace() would do just fine.

07-30-2008, 12:00 AM
ok since we are on the subject, how could you make the script secure? like using mysql_real_escape_string on the outcome, because it mucks it up.