fightstarr20
fightstarr20

Reputation: 12568

Extract Image SRC from string using preg_match_all

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?

Upvotes: 2

Views: 3530

Answers (3)

mickmackusa
mickmackusa

Reputation: 47864

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',
)

Upvotes: 0

Brad Werth
Brad Werth

Reputation: 17647

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.

Upvotes: 1

Matt Whipple
Matt Whipple

Reputation: 7134

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.

Upvotes: 1

Related Questions