Log in

View Full Version : ereg_replace multiple strings problem



JShor
09-01-2007, 07:53 PM
Hi, I have a script that has special characters that need to be replaced with an image, to be inserted into mysql. I use ereg_replace, but it will only replace one string, when I have multiple strings that need to be replace, before it gets inserted into mysql. the code looks like this:



<?php

$title = $_POST['title'];
$message2 = $_POST['message'];
$uname = $_COOKIE['uname'];

if($a == 'a'){
mysql_query("INSERT INTO topics (title, message, uname) VALUES('$title', '$message', '$uname') ") or die(mysql_error());

//Notice how it will replace this string, but I have more than one
//string like ":D" (made to represent emoticons)

$smstring = "<img src='laugh.gif'>";

$message = ereg_replace(":D", "$smstring", "$message2");

}else{

?>
<form action="post.php?a=a" method="post">
<input type="text" name="title" value="title"><br />
<textarea name="message" value="message"><br />
<input type="submit" name="submit">
</form>
<?php
}
?>


Of course, it does work, but it will only replace one string with emoticons...I tried repeating the variable ( $message ) twice, but all failed...how do I get it to change multiple strings?? :confused:

Twey
09-01-2007, 08:04 PM
Because you haven't applied the global modifier. However, regex is a waste of resources here; it would be better to do
$message = str_replace(':D', $smstring, $message2);