Regex for url and image in text or html

I'm running out of ideas on the best regex implementation for this problem.

User input example:

  bla bla bla http://foo.com bla bla bla http://tinypic.com/boo.png bla bla bla 

You are looking for a solution that will detect a URL without an image and turn it into a link, and also turn the image URL into an embeddable image (IMG tag).

so the output will be:

  bla bla bla <a href="http://foo.com"> http://foo.com </a> bla bla bla <img src = "http://tinypic.com/boo.png" /> bla bla bla 

Related

  • Regex for validating a URL that ends in .jpg, .png or .gif
  • What is the best regex to check if a string is correct?
  • Retrieving URL Parts (Regex)
+1
url php regex image
source share
2 answers

Break it into several steps.

First find links with something like:

/http:[^ ,]+/i 

Then replace the corresponding string with new content based on the type you can find by matching the string you want to replace, for example:

 /\.(jpg|png|gif|bmp)$/i 
+4
source share

Looking for a solution that will detect a URL without an image

There is no such thing as an image or a URL without an image. The presence (or absence) of the .gif / file extension. Jpeg /. Jpg / .png has nothing to do with whether the URL points to an image or not.

eg. " http://www.example.com/getimage/3 " can return a perfectly good JPEG. It is not possible to find out on which medium the URL is indicated without receiving it (using GET or, better, HEAD) and checking that the returned "Content-Type is" image / something.

0
source share

All Articles