turn text into link

0

I'm trying to transform the link of an image into the image within the tag, however I'm having difficulties when the image exists, the same I try to do with the link

In case I use the code below

$reply = preg_replace('#((http://|https://|//)([^\s]*)\.jpg|gif|png|JPG))#',  
'<img src="$1" alt="" width="" height="" />', $data['reply']);

It works great when the link is posted like this

http://wallpaper.ultradownloads.com.br/276255_Papel-de-Parede-Meme-Obama-Not-Bad_1280x1024.jpg 

but if there is something like this in the text

<img src="http://wallpaper.ultradownloads.com.br/276255_Papel-de-Parede-Meme-Obama-Not-Bad_1280x1024.jpg" > 

It will try to transform too, how could I do?

    
asked by anonymous 14.10.2014 / 00:50

2 answers

3

It is a suggestion to change the code.

Test like this:

preg_match('/(https?:\/\/[^\s\'\"]*)/', $data['reply'], $match);
$reply = '<img src="'.$match[0].'" alt="" width="" height="" />';

Example: link

I find this way cleaner. First extract the url you want, then concatenate it in the string.

    
14.10.2014 / 01:43
0

If you understand correctly what you want to do is take the url only from "anchor" tags and when you run the code it takes everything that is url.

So you just need to make this little change to the code that Sergio posted above

preg_match('/href="(https?:\/\/[^\s\'\"]*)/', $data['reply'], $match);
$reply = '<img src="'.$match[1].'" alt="" width="" height="" />';
    
14.10.2014 / 14:23