Matching Src Attribute of Img Tag Using Preg_Match

Matching SRC attribute of IMG tag using preg_match

Your expression is incorrect. Try:

preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $row->introtext, $matches);

Note the removal of brackets around img and src and some other cleanups.

Extract img src from string with preg_match_all

<img[^>]*src="([^"]*/temp/temp[a-z0-9]{13}\.jpeg)"

<img[^>]* Select IMG tags

src="([^"]*)" gets src value and save it as a match

/temp/temp[a-z0-9]{13}\.jpeg is the filter for src values

For quick RegEx tests use some online tool like http://regexpal.com/

Preg_match to find img src in specific img-tag

This regex should work:

preg_match('/<img[^>]*itemprop="photo"[^>]*src="([^"]+)">/',$source,$matches);

An explanation of the regex (from regex101):

Explanation of the regex

The result will be in the array $matches.

php preg_match to find img tag but not with gif extension

Don't do it that way. Attempting to parse HTML with regex is a task doomed to failure, since a slight increase in the complexity of the HTML or the requirement will make your regex unbelievably complicated.

The best way is to use a tool designed for the task: the DOMDocument class.

$dom = new DOMDocument;
$dom->loadHTML($text);

$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
if (!substr($image->getAttribute('src'), -4) === '.gif') {
break;
}
}

// $image is now the first image that didn't end with .gif

Regex & PHP - isolate src attribute from img tag

If you don't wish to use regex (or any non-standard PHP components), a reasonable solution using the built-in DOMDocument class would be as follows:

<?php
$doc = new DOMDocument();
$doc->loadHTML('<img src="http://example.com/img/image.jpg" ... />');
$imageTags = $doc->getElementsByTagName('img');

foreach($imageTags as $tag) {
echo $tag->getAttribute('src');
}
?>

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

Thank every one for helping me out.

I found my solution by using:

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

Extract Image SRC from string using preg_match_all

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

preg_match() not matching img src

Don't use regexes for parsing HTML. There are tools designed for this very thing.

You can use DOMDocument to parse the HTML and then easily get the value of the src attribute:

$previous_value = libxml_use_internal_errors(true);
$string = 'This is an iamge: <img src="images/Christmas.PNG" width=70%; height=40%">';
$dom = new DOMDocument();
$dom->loadHTML($string);
echo $dom->getElementsByTagName('img')->item(0)->getAttribute("src");
libxml_clear_errors();
libxml_use_internal_errors($previous_value);

Demo



Related Topics



Leave a reply



Submit