You are invited to Log in or Register a free Frihost Account!

str_replace (smilies for my guestbook)

I read some tutorials to get the simly thing done but I think I wasted time with it, this is the code to place the smilies:


function replacesmiley($msg)
$msg = str_replace(':)', '<img src="Images/smilies/1.gif" alt=":)" />', $msg);
$msg = str_replace(':-)', '<img src="Images/smilies/1.gif" alt=":-)" />', $msg);
$msg = str_replace(':(', '<img src="Images/smilies/2.gif" alt=":)"  />', $msg);
$msg = str_replace(':-(', '<img src="Images/smilies/2.gif" alt=":-)"  />', $msg);
$msg = str_replace(';)', '<img src="Images/smilies/3.gif" alt=":)" />', $msg);
$msg = str_replace(';-)', '<img src="Images/smilies/3.gif" alt=":-)" />', $msg);
$msg = str_replace(':D', '<img src="Images/smilies/4.gif" alt=":D" />', $msg);
$msg = str_replace(':-D', '<img src="Images/smilies/4.gif" alt=":-d" />', $msg);
$msg = str_replace(':d', '<img src="Images/smilies/4.gif" alt=":D" />', $msg);
$msg = str_replace(':-d', '<img src="Images/smilies/4.gif" alt=":-d" />', $msg);
$msg = str_replace(':P', '<img src="Images/smilies/5.gif" alt=":P" />', $msg);
$msg = str_replace(':-P', '<img src="Images/smilies/5.gif" alt=":-P" />', $msg);
$msg = str_replace(':p', '<img src="Images/smilies/5.gif" alt=":P" />', $msg);
$msg = str_replace(':-p', '<img src="Images/smilies/5.gif" alt=":-p" />', $msg);
$msg = str_replace(':S', '<img src="Images/smilies/6.gif" alt=":S" />', $msg);
$msg = str_replace(':-S', '<img src="Images/smilies/6.gif" alt=":-s" />', $msg);
$msg = str_replace(':s', '<img src="Images/smilies/6.gif" alt=":S" />', $msg);
$msg = str_replace(':-s', '<img src="Images/smilies/6.gif" alt=":-s" />', $msg);
$msg = str_replace(':|', '<img src="Images/smilies/7.gif" alt=":|" />', $msg);
$msg = str_replace(':-|', '<img src="Images/smilies/7.gif" alt=":-|" />', $msg);
$msg = str_replace(':*(', '<img src="Images/smilies/8.gif" alt=":*(" />', $msg);
$msg = str_replace(':O', '<img src="Images/smilies/9.gif" alt=":O" />', $msg);
$msg = str_replace(':-O', '<img src="Images/smilies/9.gif" alt=":-O" />', $msg);
$msg = str_replace(':o', '<img src="Images/smilies/9.gif" alt=":o" />', $msg);
$msg = str_replace(':-o', '<img src="Images/smilies/9.gif" alt=":-o" />', $msg);
$msg = str_replace('(A)', '<img src="Images/smilies/10.gif" alt="(A)" />', $msg);
$msg = str_replace('^O)', '<img src="Images/smilies/11.gif" alt="^O)" />', $msg);
$msg = str_replace('^o)', '<img src="Images/smilies/11.gif" alt="^o)" />', $msg);
$msg = str_replace('(H)', '<img src="Images/smilies/12.gif" alt="(H)" />', $msg);
$msg = str_replace('(FE)', '<img src="Images/smilies/13.gif" alt="(FE)" />', $msg);
$msg = str_replace(':@', '<img src="Images/smilies/14.gif" alt=":@" />', $msg);
$msg = str_replace(':-@', '<img src="Images/smilies/14.gif" alt=":-@" />', $msg);
$msg = str_replace('(6)', '<img src="Images/smilies/15.gif" alt="(6)" />', $msg);
$msg = str_replace(':$', '<img src="Images/smilies/16.gif" alt=":$" />', $msg);
$msg = str_replace(':-$', '<img src="Images/smilies/16.gif" alt=":-$" />', $msg);
$msg = str_replace('+o(', '<img src="Images/smilies/17.gif" alt="+o(" />', $msg);
$msg = str_replace('(L)', '<img src="Images/smilies/18.gif" alt="(L)" />', $msg);
$msg = str_replace('(U)', '<img src="Images/smilies/19.gif" alt="(U)" />', $msg);
$msg = str_replace('(K)', '<img src="Images/smilies/20.gif" alt="(K)" />', $msg);
$msg = str_replace('(B)', '<img src="Images/smilies/21.gif" alt="(B)" />', $msg);
$msg = str_replace('({)', '<img src="Images/smilies/22.gif" alt="({)" />', $msg);
$msg = str_replace('(})', '<img src="Images/smilies/23.gif" alt="(})" />', $msg);
$msg = str_replace('(Z)', '<img src="Images/smilies/24.gif" alt="(Z)" />', $msg);
$msg = str_replace('(X)', '<img src="Images/smilies/25.gif" alt="(X)" />', $msg);
$msg = str_replace('(8)', '<img src="Images/smilies/26.gif" alt="(8)" />', $msg);
$msg = str_replace('(I)', '<img src="Images/smilies/27.gif" alt="(I)" />', $msg);
$msg = str_replace('(Y)', '<img src="Images/smilies/28.gif" alt="(Y)" />', $msg);
$msg = str_replace('(N)', '<img src="Images/smilies/29.gif" alt="(N)" />', $msg);
$msg = str_replace('(FU)', '<img src="Images/smilies/30.gif" alt="(FU)" />', $msg);
return $msg;

I thought that this could be easier with a database,,, I tryed to generate the above with a mysql_fetch_row but it didn't worked :S

anyone an idea how to do it? I got the following in the DB: id, Code (ie :)) and URL (ie Images/smilies/1.gif).

hmm before I forget this it what I used:

$con = mysql_connect("localhost","root");
mysql_select_db("golles", $con);

$result = mysql_query("SELECT * FROM smilies ORDER BY id", $con);

function replacesmiley($msg)
while (list($id, $URL, $Code) =
$msg = str_replace($Code, '<img src="' . $URL . '" alt="'. $Code . '" />', $msg);
return $msg;

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Documents and Settings\golles\Mijn documenten\Website\golles\gastenboek.php on line 44

I know I made a damn long message, but I think everythig is needed,,,
anway, all help will be appriciated!
(btw posting a tutorial of howto do this would also be fine

your code looks good. I think you have a problem with mysql.
Have a look at this site:
I have encountered this problem many a times, but it has always got rectified but even I never had a clue as so how it happened...

Poppitz link was a cool link and this was what caught my attention:


Most of the time it just means that you could not connect to the database (for whatever reason) and thus the result set you tried to fetch was not a "valid MySQL result resource".

Acknowledging that, I have a simple solution that might work. In the 1st line of the program, when you connect to the db, add a third argument of your password as "", even though it may not be required. I think that'll help. What i mean is instead of:


$con = mysql_connect("localhost","root");

Try this:


$con = mysql_connect("localhost","root", "");
doesn't work,,,
You need to place your connction and your query inside the function. When they're outside the function and you refer to the $result from inside the function, $result will be null (or empty, as you whish) in the function.
If $result is empty, it's not a database resource, and there is your error Smile
Thus just put everything INSIDE the function.

BlackSkad wrote:
You need to place your connction and your query inside the function. When they're outside the function and you refer to the $result from inside the function, $result will be null (or empty, as you whish) in the function.
If $result is empty, it's not a database resource, and there is your error Smile
Thus just put everything INSIDE the function.


BlackSkad is right. Put everything inside the function, or pass the result to it (or use the "dreaded" global keyword).

That said, using a database for storing smiley's seems a bit overkill - retrieving 20-30 rows to do a simple replace of one string... Generally, if your data isn't meant to be dynamic (I don't expect you to change your smilies every other day), keep it in your code, not a database.

What keeps you from doing an array like this:

$smilies = array(
  ':)' => 1,
  ':-)' => 1,
  ':(' => 2,
  ':-(' => 2,


function replacesmilies($msg, $smilies) {
  foreach($smilies as $smiley => $id) {
    $msg = str_replace($smiley, '<img src="Images/smilies/' . $id .'.gif" alt="' . $smiley . '" />', $msg);

(example code, not tested)

? Smile
Related topics
New smilies
Fireworks - Absolutely kickass smilies!
problem on advance guestbook
can you help me?
the simple guestbook
Viper Guestbook
New smilies
Turn off smilies.
How to see my Guestbook ?
guestbook for frihost
script of guestbook in PHP
Need help - guestbook pagination like [1] [2] [3] etc.
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

© 2005-2011 Frihost, forums powered by phpBB.