daveofwar Posted October 28, 2007 Share Posted October 28, 2007 Does anyone know how to get the content of src in the html tag img. For example <img src="http://path.to/img" id="randomid" /> In the example the string i would like to get would be http://path.to/img Thanks Link to comment https://forums.phpfreaks.com/topic/75149-get-content-of-src-in-img-tag-php-and-regex/ Share on other sites More sharing options...
toplay Posted October 29, 2007 Share Posted October 29, 2007 There's probably a better way to do it, but this is off the top of my head for now: <?php $html = '<img src="http://path.to/img" id="randomid" />'; if (preg_match('/<img.+?src(?: )*=(?: )*[\'"](.*?)[\'"]/si', $html, $arrResult)) { echo $arrResult[1]; // Should display http://path.to/img } else { echo "No match found"; } ?> Link to comment https://forums.phpfreaks.com/topic/75149-get-content-of-src-in-img-tag-php-and-regex/#findComment-380135 Share on other sites More sharing options...
effigy Posted October 29, 2007 Share Posted October 29, 2007 /<img[^>]+src=([\'"])?((?(1).+?|[^\s>]+))(?(1)\1)/ Link to comment https://forums.phpfreaks.com/topic/75149-get-content-of-src-in-img-tag-php-and-regex/#findComment-380349 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.