...

View Full Version : Problem replacing smilies



fael097
03-19-2010, 08:39 PM
hello, i have a really simple code for replacing a string message containing smilies symbols for smilies images. heres my code:


<?php
include("../connect.php");

$messagein= "smilies: =) =D =( (!)";

$smilies=mysql_query("SELECT * FROM smilies")or die(mysql_error());

while ($get=mysql_fetch_array($smilies))
{
$symbol=$get['symbol'];
$smilie=$get['url'];
$img="<img src='../shoutbox/smilies/".$smilie."' border='0' width='15' height='15'>";
$message=str_replace($symbol, $img, $messagein);
}
echo $message;
?>

as it its, with the echo outside the while loop, it will only replace one smilie, the last in the mysql smilies table.
for obvious reasons, if i put the echo inside the while loop, it will repeat the message as many times as how many smilies i have in the mysql smilies table, what i dont want, i just want the message to show once, replacing all the smilies. how to do that?!

here's a list of my available smilies http://fael097.site11.com/test/test6.php
and here's my code i posted above, as its (not)working right now http://fael097.site11.com/test/test5.php

thank you in advance

mlseim
03-19-2010, 09:01 PM
There are some PHP emoticon classes you can use.
I also found this:
http://www.webcodez.net/php-mysql/php-smilies-system/

Maybe you want to program your own, but if there's already scripts available ...

SKDevelopment
03-19-2010, 11:22 PM
E.g. you could store patterns and replacements in arrays and then use these arrays in str_replace() after the while loop. Something like this:


<?php
include("../connect.php");

$messagein= "smilies: =) =D =( (!)";

$smilies=mysql_query("SELECT * FROM smilies")or die(mysql_error());

$symbol = array();
$img = array();
while ($get=mysql_fetch_array($smilies))
{
$symbol[]=$get['symbol'];
$smilie=$get['url'];
$img[]="<img src='../shoutbox/smilies/".$smilie."' border='0' width='15' height='15'>";
}
$message=str_replace($symbol, $img, $messagein);
echo $message;
?>

fael097
03-22-2010, 01:08 PM
thanks a lot bud, thats exactly what i was looking for.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum