1

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];
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1055774
  • 131
  • 1
  • 4
  • 9
  • 1
    [The pony he comes....](http://stackoverflow.com/a/1732454/554546) –  Jan 30 '12 at 18:15
  • 1
    possible duplicate of [RegEx match open tags except XHTML self-contained tags](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) –  Jan 30 '12 at 18:15

3 Answers3

3

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Seagull
  • 3,319
  • 2
  • 31
  • 37
1

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
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
FtDRbwLXw6
  • 27,774
  • 13
  • 70
  • 107
1

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."

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
buley
  • 28,032
  • 17
  • 85
  • 106