Page 1 of 1
replace with smilie
Posted: Tue Dec 30, 2003 6:47 pm
by thomasd1
hi,
check it out:
Code: Select all
<?php
require 'db_info.php';
$db = mysql_connect($dbhost,$dbuser);
mysql_select_db($mydb,$db);
$result = mysql_query("SELECT * FROM guestbook ORDER BY id DESC",$db);
while ($rows=mysql_fetch_row($result)){
$message = $rows[4];
//replace with <img src="../img/smillies/smile.gif">
str_replace(":)","<img src="../img/smillies/smile.gif">",$message);
echo "<tr><td class="gbhead" width="100%">By <a href="mailto:" . $rows[2] . "">" . $rows[1] . "</a> | " . $rows[3] . "</td></tr>";
echo "<tr><td class="gbtext">" . $message . "</td></tr>";
}
?>
it doesn't work ...

Posted: Tue Dec 30, 2003 6:49 pm
by Gen-ik
It should be...
Code: Select all
$message = str_replace(":)","<img src="../img/smillies/smile.gif">",$message);
Posted: Tue Dec 30, 2003 6:56 pm
by Gen-ik
Just a quick tip... if you have loads of smilies you might want to do something like this...
Code: Select all
<?php
$face[] = ":)"; $img[] = "happy";
$face[] = ":("; $img[] = "sad";
$face[] = ";)"; $img[] = "smug";
$face[] = ";("; $img[] = "angry";
foreach($face as $key => $value)
{
$html = "<img src="smilies/".$img[$key].".gif">";
$string = str_replace($value, $html, $string);
}
?>
Posted: Wed Dec 31, 2003 4:10 am
by thomasd1
wow thanks verymuch!
Posted: Wed Dec 31, 2003 5:45 am
by thomasd1
Why won't this work?
Code: Select all
<?php
function emoticon($message){
$message = str_replace(":)","<img src="../img/smillies/icon_smile.gif">",$message);
$message = str_replace(":(","<img src="../img/smillies/icon_sad.gif">",$message);
$message = str_replace("':?","<img src="../img/smillies/icon_confused.gif">",$message);
$message = str_replace("8)","<img src="../img/smillies/icon_cool.gif">",$message);
}
?>
<?php
require 'db_info.php';
$db = mysql_connect($dbhost,$dbuser);
mysql_select_db($mydb,$db);
$result = mysql_query("SELECT * FROM guestbook ORDER BY id DESC",$db);
while ($rows=mysql_fetch_row($result)){
$message = $rows[4];
emoticon($message);
echo "<tr><td class="gbhead" width="100%">By <a href="mailto:" . $rows[2] . "">" . $rows[1] . "</a> | " . $rows[3] . "</td></tr>";
echo "<tr><td class="gbtext">" . $message . "</td></tr>";
}
?>

Posted: Wed Dec 31, 2003 5:50 am
by qads
Code: Select all
<?php
function emoticon($message){
$message = str_replace(":)","<img src="../img/smillies/icon_smile.gif">",$message);
$message = str_replace(":(","<img src="../img/smillies/icon_sad.gif">",$message);
$message = str_replace("':?","<img src="../img/smillies/icon_confused.gif">",$message);
$message = str_replace("8)","<img src="../img/smillies/icon_cool.gif">",$message);
return $message;
}
?>
you have to return $message from the function.
Posted: Wed Dec 31, 2003 5:52 am
by Weirdan
thomasd1 wrote:Why won't this work?
Code: Select all
<?php
function emoticon($message){
$message = str_replace(":)","<img src="../img/smillies/icon_smile.gif">",$message);
$message = str_replace(":(","<img src="../img/smillies/icon_sad.gif">",$message);
$message = str_replace("':?","<img src="../img/smillies/icon_confused.gif">",$message);
$message = str_replace("8)","<img src="../img/smillies/icon_cool.gif">",$message);
}
?>

because the $message is passed 'by value'. either pass it by reference:
Code: Select all
function emoticon(&$message){
//...skipped
or return it from your function:
Code: Select all
//....skipped
$message = str_replace("8)","<img src="../img/smillies/icon_cool.gif">",$message);
return $message;
}
//....skipped
$message=emoticon($message);
//....skipped
more info
Posted: Wed Dec 31, 2003 6:19 am
by thomasd1
i should've known
thanks