Extract Image SRC from string using preg_match_all

无人久伴 提交于 2019-12-11 01:54:04

问题


I have a string of data that is set as $content, an example of this data is as follows

This is some sample data which is going to contain an image in the format <img src="http://www.randomdomain.com/randomfolder/randomimagename.jpg">.  It will also contain lots of other text and maybe another image or two.

I am trying to grab just the <img src="http://www.randomdomain.com/randomfolder/randomimagename.jpg"> and save it as another string for example $extracted_image

I have this so far....

if( preg_match_all( '/<img[^>]+src\s*=\s*["\']?([^"\' ]+)[^>]*>/', $content, $extracted_image ) ) {
$new_content .= 'NEW CONTENT IS '.$extracted_image.'';

All it is returning is...

NEW CONTENT IS Array

I realise my attempt is probably completly wrong but can someone tell me where I am going wrong?


回答1:


Using regex to parse valid html is ill-advised. Because there can be unexpected attributes before the src attribute, because non-img tags can trick the regular expression into false-positive matching, and because attribute values can be quoted with single or double quotes, you should use a dom parser. It is clean, reliable, and easy to read.

Code: (Demo)

$string = <<<HTML
This is some sample data which is going to contain an image
in the format <img src="http://www.randomdomain.com/randomfolder/randomimagename.jpg">.
It will also contain lots of other text and maybe another image or two
like this: <img alt='another image' src='http://www.example.com/randomfolder/randomimagename.jpg'>
HTML;

$srcs = [];
$dom=new DOMDocument;
$dom->loadHTML($string);
foreach ($dom->getElementsByTagName('img') as $img) {
    $srcs[] = $img->getAttribute('src');
}

var_export($srcs);

Output:

array (
  0 => 'http://www.randomdomain.com/randomfolder/randomimagename.jpg',
  1 => 'http://www.example.com/randomfolder/randomimagename.jpg',
)



回答2:


Your first problem is that http://php.net/manual/en/function.preg-match-all.php places an array into $matches, so you should be outputting the individual item(s) from the array. Try $extracted_image[0] to start.




回答3:


You need to use a different function, if you only want one result:

preg_match() returns the first and only the first match. preg_match_all() returns an array with all the matches.



来源:https://stackoverflow.com/questions/12450943/extract-image-src-from-string-using-preg-match-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!