I'm trying to extract the first src attribute of an image in a block of HTML text like this:
Lorem ipsum <img src="http://example.com/img.jpg" />consequat.
I have no problem creating the regular expression to match the src attribute, but how do I return the first matched src attribute, instead of replacing it?
From pouring over the PHP manual, it seems like preg_filter() would do the trick, but I can't rely on end users having a PHP version greater than 5.3.
All the other PHP regular expression functions seem to be variations of preg_match(), returning a Boolean value, or preg_replace, which replaces the match with something. Is there a straightforward way to return a regular expression match in PHP?
You can use the third parameter of preg_match
to know what was matched (it's an array, passed by reference):
int preg_match ( string $pattern ,
string $subject [, array &$matches [,
int $flags [, int $offset ]]] )
If matches is provided, then it is filled with the results of search.
$matches[0]
will contain the text that matched the full pattern,$matches[1]
will have the text that matched the first captured parenthesized subpattern, and so on.
For instance, with this portion of code:
$str = 'Lorem ipsum dolor sit amet, adipisicing <img src="http://example.com/img.jpg" />consequat.';
$matches = array();
if (preg_match('#<img src="(.*?)" />#', $str, $matches)) {
var_dump($matches);
}
You'll get this output:
array
0 => string '<img src="http://example.com/img.jpg" />' (length=37)
1 => string 'http://example.com/img.jpg' (length=23)
(Note that my regex is overly simplistic -- and that regex are generally not "the right tool" when it comes to extracting data from some HTML string...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With