smiley to image - optimized PHP code -


is code or can optimized ?

 $tempmsg = str_replace("[:)]","<img src='img/smiley0.png' title='smile' height='100' width='100'>",$tempmsg); 

1/ can optimized?

it pretty good, you're not using regular expressions (preg_replace) no worries here.

2/ code

to me not readable. how if need replace other smileys? not reusable. here first step towards better readability, increased reusability, ... might bit slower, tend favor reusability on performance until need optimize.

function replace_smiley($text) {   $replacements = array(       ":)" => "image1.jpg",       ":(" => "image2.jpg"   );    $out = $text;   foreach ($replacements $code => $image) {     $html = '<img src="img/' . $image . ' alt="' . $code . '" height="100" width="100" />';     $out = str_replace($code, $html, $out);   }   return $out; } 

of course, depending on requirements, there room improvement.


Comments

Popular posts from this blog

how to build hyperlink for query string in php -

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

queue - mq_receive: message too long -