golles Posted June 7, 2006 Share Posted June 7, 2006 I read some tutorials to get the smily thing done but I think I wasted time with it, this is the code to place the smilies:[code]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;}[/code]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 :Sanyone 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:[code]<?php$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) =mysql_fetch_row($result)){$msg = str_replace($Code, '<img src="' . $URL . '" alt="'. $Code . '" />', $msg);}return $msg;}?>[/code][!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]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[/quote]I know I made a damn long message, but I think everythig is needed,,,anway, all help will be appriciated!(btw posting something to do this on a beter way is also good!//golles Quote Link to comment https://forums.phpfreaks.com/topic/11421-str_replace/ Share on other sites More sharing options...
wildteen88 Posted June 7, 2006 Share Posted June 7, 2006 You need to make your $result variable global in your function. So chnage this:[code]function replacesmiley($msg){[/code]to this:[code]function replacesmiley($msg){ global $result;[/code]Variables dont have global scope when used in a function so you need to declare the variables that are defined out side of the function as global Quote Link to comment https://forums.phpfreaks.com/topic/11421-str_replace/#findComment-42839 Share on other sites More sharing options...
golles Posted June 7, 2006 Author Share Posted June 7, 2006 that won't help, now it will only give me the last message from the guestbook, and it doesn't replace the smilies with images, do you know a beter way to get smilies on the guestbook page? (with the SQL database if posible??)? Quote Link to comment https://forums.phpfreaks.com/topic/11421-str_replace/#findComment-42844 Share on other sites More sharing options...
wildteen88 Posted June 7, 2006 Share Posted June 7, 2006 Ok. What is you current setup. How are you getting your guestbook records out of the database? Could you post the code here. Quote Link to comment https://forums.phpfreaks.com/topic/11421-str_replace/#findComment-42845 Share on other sites More sharing options...
obsidian Posted June 7, 2006 Share Posted June 7, 2006 try something like this:[code]<?php$sql = mysql_query("SELECT * FROM smilies ORDER BY id");$smilies = array();$replace = array();while ($x = mysql_fetch_array($sql)) { $smilies[] = $x['Code']; $replace[] = "<img src=\"$x[URL]\" />";}$String = str_replace($smilies, $replace, $String);?>[/code]this way, you can load up your arrays from your query, but you only have to do your str_replace() once... much more efficient Quote Link to comment https://forums.phpfreaks.com/topic/11421-str_replace/#findComment-42870 Share on other sites More sharing options...
golles Posted June 7, 2006 Author Share Posted June 7, 2006 [!--quoteo(post=381071:date=Jun 7 2006, 07:12 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Jun 7 2006, 07:12 PM) [snapback]381071[/snapback][/div][div class=\'quotemain\'][!--quotec--]try something like this:[code]<?php$sql = mysql_query("SELECT * FROM smilies ORDER BY id");$smilies = array();$replace = array();while ($x = mysql_fetch_array($sql)) { $smilies[] = $x['Code']; $replace[] = "<img src=\"$x[URL]\" />";}$String = str_replace($smilies, $replace, $String);?>[/code]this way, you can load up your arrays from your query, but you only have to do your str_replace() once... much more efficient[/quote]ok, thank you, I was testing but I'm assuming I do something wrong,,,here is the php part,,, please tell me what I'm doing wrong[code]<?phpfunction replacesmiley($String){$con = mysql_connect("localhost","root", "");mysql_select_db("golles", $con);$sql = mysql_query("SELECT * FROM smilies ORDER BY id");$smilies = array();$replace = array();while ($x = mysql_fetch_array($sql)) { $smilies[] = $x['Code']; $replace[] = "<img src=\"$x[URL]\" />";}$String = str_replace($smilies, $replace, $String);return $String;}$con = mysql_connect("localhost","root", "");mysql_select_db("golles", $con);$result = mysql_query("SELECT * FROM gastenboek ORDER BY id DESC", $con);while (list($id, $naam, $mail, $site, $datum, $ip, $bericht) =mysql_fetch_row($result)){$naam = htmlspecialchars($naam);$mail = htmlspecialchars($mail);$site = htmlspecialchars($site);$bericht = htmlspecialchars($bericht);$bericht = replacesmiley($bericht);$bericht = nl2br($bericht);if ($site == 'http://'){ $site = ''; } if ($mail == ''){$naammail = $naam;} else {$naammail = '<a href="mailto:' . $mail . '" class="GB">' . $naam . '</a>'; }echo ('<table class="gastenboek" align="center"><tr><td class="schrijfkop" width="40%">Naam: ' . $naammail . '</td><td class="schrijfkop" width="45%"><a href="' . $site . '" target="_blank" class="GB">' . $site . '</a></td><td class="schrijfkop">' . $datum . '</td></tr><tr><td colspan="3">' . $bericht . '</td></tr></table><br><br>');}?>[/code]it doesn't give any errors it doesn't replace the smilies Quote Link to comment https://forums.phpfreaks.com/topic/11421-str_replace/#findComment-42914 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.