Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to find "src" attribute of HTML "img" element in PHP

Tags:

html

regex

php

I have a string, inside of that I have an image:

"<p><img src="http://yahoo.com/testfolder/userdata/editoruploadimages/confused man.jpg" /></p>"

I could not fetch the image URL with my regular expression. My code is:

preg_match_all("/src=([^\\s]+)/", $questArr_str, $images);

This code stops its execution when it encounters the space in the image name. It only returns "http://yahoo.com/testfolder/userdata/editoruploadimages/confused

The returned string should be: "http://yahoo.com/testfolder/userdata/editoruploadimages/confused man.jpg"

like image 398
Santanu Avatar asked Nov 29 '22 15:11

Santanu


2 Answers

I'd catch everything inside the quotes:

preg_match_all('/src="([^"]+)"/', $questArr_str, $images);
like image 85
Beat Avatar answered Dec 05 '22 22:12

Beat


The parts that reads ([^\s]+) means select anything that isn't a space.

Maybe try something like:

/src="([^"]+)"/

Which is select anything that isn't a double quote.

like image 28
Adrian Thompson Phillips Avatar answered Dec 05 '22 23:12

Adrian Thompson Phillips