Search code examples
phpregeximageurl

Replace all image urls with <img> tags


I want te replace all image links on image tags, but I don't know what I'm doing wrong.

Here is my regex: /(http:\/\/.+(png|jpeg|jpg|gif|bmp))/i

But when i use it with string: http://re-actor.net/uploads/posts/2009-08/1249921580_toyota-ft-hs.png and http://re-actor.net/uploads/posts/2009-08/thumbs/1249922220_toyota_ft-hs-14.jpg

Result is <img src="http://re-actor.net/uploads/posts/2009-08/1249921580_toyota-ft-hs.png and http://re-actor.net/uploads/posts/2009-08/thumbs/1249922220_toyota_ft-hs-14.jpg" />

(one image tag for two links)

How to fix it?


Solution

  • It's because your regex has to be ungreedy. Just add the U flag at the end of your regex:

    /(http:\/\/.+(png|jpeg|jpg|gif|bmp))/Ui
    

    And it works!