Reputation: 131
I am struggling to get preg_match to return only the image URL and not the entire matched string. Do I need to use preg_replace after or is that getting hacky?
Possibly, would a different syntax get me what I need?
$source = file_get_contents('http://mysite.co.uk');
preg_match_all("'<div id=\"my-div\"><img src=\"(.*?)\" /></div>'", $source, $match);
echo $match[0][0];
Upvotes: 1
Views: 6406
Reputation: 29208
By default, preg_match_all
always returns the fully matched string as the first item (using the ordering type PREG_PATTERN_ORDER
).
From the documentation for PREG_PATTERN_ORDER
:
Orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on.
If you're looking for the value of a capturing group, check for a value at index 1 and then use the capturing group reference as a subattribute.
E.g., capturing group 1 would be: $matches[1][0]
To change this behavior you can pass a constant to as the third argument, such as PREG_SET_ORDER
, which "Orders results so that $matches[0] is an array of first set of matches, $matches[1] is an array of second set of matches, and so on."
Upvotes: 1
Reputation: 3600
If you use echo $match[0][0]
you will have all the text.
<div id="my-div"><img src="blabla bla" /></div>
If you write $match[1][0]
instead, you will get your subpattern match:
blabla bla
Upvotes: 3
Reputation: 28899
If you're looking for the first instance, you don't need to use preg_match_all()
:
$source = file_get_contents('http://mysite.co.uk');
if (preg_match('#<div id="my-div"><img src="(.*?)" /></div>#', $source, $match)) {
echo $match[1];
} else {
// no match found
}
Note that this regex will not match across multiple lines.
If you need all matches, then you were on the right track, but you were using index 0
instead of 1
, so:
preg_match_all(..., $match);
foreach ($match as $m) {
echo $m[1]; // Use 1 here instead of 0; 1 is the first capture group, where 0 is the entire matched string
}
Upvotes: 1