Pepito the intern
Pepito the intern

Reputation: 15

How to transform IMG(url.jpg) into html img using php?

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

Answers (2)

r3mainer
r3mainer

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

PHPeter
PHPeter

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

Related Questions