turn text into link

Asked

Viewed 877 times

0

I am trying to turn the link of an image into the image inside the tag, however I am having difficulties when there is the image, 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 the text has something like this

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

he’ll try to turn too, as I might do?

  • As a curiosity, a very good site to check the regex: http://www.regexr.com/

2 answers

3

A suggested code change.

Test like this:

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

Example: http://ideone.com/qpknsU

I find this way cleaner. First extract the desired url, then concatenate into the string.

  • But the text has but things beyond the URL including other URL’s that are not images and images within tags

  • @Daniellemes, in this case put more examples for me to adjust the answer

0

If I understand well what you want is to take the url only of "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 on top

preg_match('/href="(https?:\/\/[^\s\'\"]*)/', $data['reply'], $match);
$reply = '<img src="'.$match[1].'" alt="" width="" height="" />';
  • No, I intend to take a text that contains image link and return the text with the images within the img tag, in addition this text may or may not have link to url and images already within tags and this should be ignored.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.