Simon911 Simon911 - 6 months ago 24
PHP Question

PHP replace smiley by img tag

I am trying to replace smileycodes by a img tag.

I want to replace this:



<img src='images/blush.png' />

"blush" is a variabele, so it can be any smiley.

I have to replace everything between the colons. I am not familiar with regex.

Can you help me?


For multiple replacements you could use str_replace with arrays instead of strings to search for, having an array for smiley-codes to be replaced by values of the second array. But you need to configure all values in arrays what is kind of static.

Another solution was to loop over possible smiley-codes and do a str_replace for each of it:

$possibleCodes = array('blush', 'smiley2', 'smiley3');
foreach($possibleCodes as $code) {
  str_replace(':'.$code.':', "<img src='images/".$code.".png'", $yourString);

This requires to have the image name same as the smiley-code.