View Single Post
Old 12-29-2007, 04:36 AM   #2 (permalink)
deflated
The Wanderer
 
deflated's Avatar
 
Join Date: Dec 2007
Location: 127.0.0.1
Posts: 19
Thanks: 7
deflated is on a distinguished road
Default

Hello,

what do you mean by making an image clickable? Like a simple a-href link? I think your problem isn't a PHP specific issue. Anyway, I'd like to help you. Concerning your code: There are some things I'd do a bit different. For instance it would be faster to have to arrays $search and $replace that you pass to str_replace() instead of calling str_replace() for every smiley you replace by the filename. Here's the modified code. I hope there aren't any mistakes as I haven't tested it:

PHP Code:
$icons = array (
  
':)' => 'smile',
  
':(' => 'sad'
  
//...
);

//-----------------------------------------------

$search  = array();
$replace = array();

foreach (
$icons as $icon => $file) {
  
$search [] = $icon;
  
$replace[] = '<img src="http://www.talkphp.com/images/"' $icon '.gif">';
  
//if you want to have the icon clickable you need to use <a href="...">...</a>
}

$search  += array("\n"'|~~|');

//Andrew had the idea of using nl2br() Thanks!
//$replace += array('<br />', '');
$message nl2br($message);

$message str_replace($search$replace$message);

//are you using magic quotes or why do you want to strip out slashes from your message?
$message stripslashes($message); 

Last edited by deflated : 07-18-2010 at 01:47 PM.
deflated is offline  
Reply With Quote