enloz enloz - 1 year ago 65
PHP Question

PHP replace text with image

This would be an Array of data:

$array = array("sad","bow","blabla");

$input = "I love Stackoverflow! :bow: so much";

Output should be:

I love Stackoverflow <img src="smiley/bow.gif" alt=""/> so much

What would be the best and fastest way to accomplish this ?

Answer Source
$array = array(':('=>"sad",'-_-'=>"bow",'bla'=>"blabla");
$input = "I love Stackoverflow! :bow: so much";
$output = str_replace(array_keys($array), array_values($array), $input);

Edit: Sorry..didn't read it clearly.

Attempt 2:

preg_replace('`:(\w+):`', '<img src="smily/\1.gif" alt=""/>', $input);

Someting like that anyway.

Might have to use $1 in place of \1. Read the docs.