user3437098 user3437098 - 1 year ago 83
PHP Question

Replace string between two brackets and get a variable on the repleaced string

This is my input string: (Picture will variate)


This is what i want to output:

<img src="">

How can i use preg_replace or other alternatives to do this?

Answer Source

Just for sake of completeness, here's the preg_replace solution...

$patterns = array("/\[image\]/", "/\[\/image\]/");
$replacements = array("<img src=\"", "\">");

$string = "[image][/image]";

echo preg_replace($patterns, $replacements, $string);

Live Demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download