replace with smilie

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
thomasd1
Forum Commoner
Posts: 80
Joined: Sat Nov 22, 2003 2:48 pm
Location: Belgium

replace with smilie

Post 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 ... :roll:
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

It should be...

Code: Select all

$message = str_replace(":)","<img src="../img/smillies/smile.gif">",$message);
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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);
}

?>
User avatar
thomasd1
Forum Commoner
Posts: 80
Joined: Sat Nov 22, 2003 2:48 pm
Location: Belgium

Post by thomasd1 »

wow thanks verymuch!
User avatar
thomasd1
Forum Commoner
Posts: 80
Joined: Sat Nov 22, 2003 2:48 pm
Location: Belgium

Post 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>";
}
?>
:roll:
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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);
}
?>
:roll:
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
User avatar
thomasd1
Forum Commoner
Posts: 80
Joined: Sat Nov 22, 2003 2:48 pm
Location: Belgium

Post by thomasd1 »

i should've known :oops:

thanks
Post Reply