Replace text w/ Images? (smilies)<

liunx

Guest
lately I have made smilies for my shoutbox, but I'm having trouble with script to replace the text with the image. I have found various codes to do this, but none of them worked. The one I'm trying right now is this:
<?php

function convert_smilies($value){
$value=str_replace(":^_^:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/psmile1.gif'>", $value);
$value=str_replace(":-_-:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/rsweat1.gif'>", $value);
$value=str_replace(":o.o:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/rsup1.gif'>", $value);
$value=str_replace(":mad:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/rmad1.gif'>", $value);
$value=str_replace(":nerd:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/pnerd1.gif'>", $value);
$value=str_replace(":x_x:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/pdead1.gif'>", $value);
$value=str_replace(":T_T:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/pcry1.gif'>", $value);
$value=str_replace(":cool:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/pcool.gif'>", $value);
$value=str_replace(":o.O:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/pcon2.gif'>", $value);
$value=str_replace(":?:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/pcon1.gif'>", $value);
$value=str_replace(":@_@:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/gspin.gif'>", $value);
$value=str_replace(":^.^:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/psweat1.gif'>", $value);
return $value;
}

echo "<b><a href=http://www.htmlforums.com/archive/index.php/\"$url\" target=\"new\">$name</a></b>: replace_smiley($msg)<hr>\n";

?>
I understand how this would work, but for some reason it is not. I think I might not have the script in the correct position. Can anyone tell me where to put it? Or maybe do you know another code that will work?

Here's a page w/ my shout box, including the error... Chibi Cafe (<!-- m --><a class="postlink" href="http://chibi.cafeotaku.com">http://chibi.cafeotaku.com</a><!-- m -->)You need to re-do the echo statement a bit.
echo "<b><a href=http://www.htmlforums.com/archive/index.php/\"$url\" target=\"new\">$name</a></b>: " . replace_smiley($msg) . "<hr>\n";
Otherwise it looks good :)I think it would be much easier to put it in an array.


$value[0] = str_replace(":^_^:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/psmile1.gif'>", $value);
$value[1] = str_replace(":-_-:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/rsweat1.gif'>", $value);

And so on.also you are overwriting the value in the function. also you are calling the wrong function.

<?php

function convert_smilies($value){
$value .=str_replace(":^_^:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/psmile1.gif'>", $value);
$value .=str_replace(":-_-:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/rsweat1.gif'>", $value);
$value .=str_replace(".o:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/rsup1.gif'>", $value);
$value .=str_replace("", "<img src='http://www.htmlforums.com/archive/index.php/smilies/rmad1.gif'>", $value);
$value .=str_replace(":nerd:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/pnerd1.gif'>", $value);
$value .=str_replace(":x_x:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/pdead1.gif'>", $value);
$value .=str_replace(":T_T:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/pcry1.gif'>", $value);
$value .=str_replace("", "<img src='http://www.htmlforums.com/archive/index.php/smilies/pcool.gif'>", $value);
$value .=str_replace(".O:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/pcon2.gif'>", $value);
$value .=str_replace(":?:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/pcon1.gif'>", $value);
$value .=str_replace(":@_@:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/gspin.gif'>", $value);
$value .=str_replace(":^.^:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/psweat1.gif'>", $value);
return $value;
}

echo "<b><a href=http://www.htmlforums.com/archive/index.php/\"$url\" target=\"new\">$name</a></b>:". convert_smilies($msg)."<hr>\n";

?>OK, i fixed all of that, but I'm still getting errors. First it repeats my post over and over again, then it shows this error at the bottom:

Fatal error: Cannot redeclare convert_smilies() (previously declared in C:\Program Files\Apache Group\Apache2\htdocs\chibi\test\view.php:50) in C:\Program Files\Apache Group\Apache2\htdocs\chibi\test\view.php on line 50You have the same function name in another location already...No, I only have it once. :(Could you post the full code please? (in an attachment if it's very long)Yeah, here it is:

<?php
#change localhost to the sql server, user to the correct username, and pass to the correct password
$con = mysql_connect("localhost","root","")
or die("Unable to establish a connection to the database.");

#change database to the database name
$database = "shout";
$db = mysql_select_db("$database") or die("Couldn't select database $database.");

// query database to list the records

$q = "select * from shoutbox order by id desc limit 15";
$result = mysql_query($q);

while ($row = mysql_fetch_array($result))
{
$id = $row["id"];
$name=$row["name"];
$url = $row["url"];
$msg = $row["msg"];

// format how you want the shout box to display the entries here.

function convert_smilies($value){
$value .=str_replace(":^_^:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/psmile1.gif'>", $value);
$value .=str_replace(":-_-:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/rsweat1.gif'>", $value);
$value .=str_replace(":o.o:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/rsup1.gif'>", $value);
$value .=str_replace(":mad:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/rmad1.gif'>", $value);
$value .=str_replace(":nerd:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/pnerd1.gif'>", $value);
$value .=str_replace(":x_x:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/pdead1.gif'>", $value);
$value .=str_replace(":T_T:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/pcry1.gif'>", $value);
$value .=str_replace(":cool:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/pcool.gif'>", $value);
$value .=str_replace(":o_O:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/pcon2.gif'>", $value);
$value .=str_replace(":question:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/pcon1.gif'>", $value);
$value .=str_replace(":@_@:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/gspin.gif'>", $value);
$value .=str_replace(":^.^:", "<img src='http://www.htmlforums.com/archive/index.php/smilies/psweat1.gif'>", $value);
return $value;
}

echo "<b><a href=http://www.htmlforums.com/archive/index.php/\"$url\" target=\"new\">$name</a></b>:". convert_smilies($msg)."<hr>\n";

}

?>

I thought the problem might be that I need to put it in the post.php page (the page that checks all the entries n stuff) but I wasn't sure. I actually kinda tried it, but it didn't work... :(take the function out of the while loop.Oh, haha! I didn't even notice that! :dunce: Thank you! Just one more thing though... It's still repeating the entry over and over again... :(the only way I can see it repeat is if the message is in the db twice.No, it was something else, but I got it fixed now. :banana: Thanks so much everybody!! :verycool:
 
Back
Top