1

This would be an Array of data:

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

IF $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 ?

3
  • I'm PHP newbie, so I don't know where to start... any help is appreciated Commented Apr 3, 2012 at 7:00
  • or preg_replace Commented Apr 3, 2012 at 7:02
  • 1
    why would you use the array? and not just replace the ":[a-z]+:" pattern? Commented Apr 3, 2012 at 7:02

2 Answers 2

1
$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.

Sign up to request clarification or add additional context in comments.

Comments

0
//string replace example.
$input = "I love Stackoverflow! :bow: so much"; 
echo str_replace(":bow:","< img src='smiley/bow.gif' alt=''/>",$input)."<br /><br />";


//use string replace with foreach 
$array = array("sad","bow","blabla");

foreach($array as $value)
{
  $input = "I love Stackoverflow! :'".$value."': so much";
  echo str_replace(":'".$value."':","< img src='smiley/$value.gif' alt=''/>",$input);  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.