Reputation: 15
I would like to transform all the occurrences IMG(url.jpg)
into <img src="/img/url.jpg" />
, from a string, using PHP.
For now, this is my code:
$content = preg_replace_callback('??????',
function($img)
{
return '<img src="/img/'.$img[0].'" />';
}
, $content);
Upvotes: 0
Views: 52
Reputation: 24587
You don't need to use a callback. A simple preg_replace
will do:
$content = preg_replace('/IMG\(([^\)]+)\)/','<img src="/img/$1" />',$content);
Upvotes: 2
Reputation: 577
This could be a way to convert from IMG(url.jpg)
into <img src="/img/url.jpg" />
<?php
function convertIMG($img)
{
$rest = substr($img, 4, -1);
return '<img src="$rest" />';
}
$before = "IMG(url.jpg)";
$after = convertIMG($before);
?>
Upvotes: 0