- preg_replace_callback: documentation ( source)
- str_replace: documentation ( source)
<?php
$text = "[img]http://www.test.com/ abc.jpg[/img]";
$uid = 123;
$text = preg_replace_callback(
"#\[img]((?:https?|ftps?)://[^?&=\#\"<]*?\.(?:jpe?g|gif|png))\[/img]#i",
function ($m) use ($uid) {
return "[img:$uid]" . str_replace(' ', '%20', $m[1]) . "[/img:$uid]";
},
$text
);
echo $text;