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):
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
How to Decode a JSON String with Several Objects in PHP
When to Wrap Curly Braces Around a Variable
Simulate File Structure with PHP
How to Get Jquery Code Completion in Netbeans
How to Get Random Image from Directory Using PHP
How to Redirect a 404 Error in a Custom 404 Page Using Codeigniter
How to Merge Array and Preserve Keys
How to Loop Through JSON Array
Converting HTML Table to a CSV Automatically Using PHP
How to Reset MySQL Pointer Back to the First Row in PHP
Enabling Cors in Codeigniter ( Restserver by @Chriskacerguis )
How to Use Andwhere and Orwhere in Doctrine
Difference Between Set_Time_Limit() and Ini_Set('Max_Execution_Time', ...)