Thanks for the response, effigy.
I think I understand now (although, admittedly, using the 'here document' and HTML sample with tags might not be the best example as tags are still parsed by the browser).
So if I understand correctly (and feel free to correct me if I'm wrong)..
In your last code snippet, when only using (.+) (which is greedy), the match is as follows after the initial </h3>? (don't mind the improper spacing / formatting here...)
United States
</td>
<td>
<h3>00CA - bluestone (GTS)
United States
If this is correct, I suppose due to browsers parsing the HTML tags, we only see the following onscreen (which is what I got):
United States
00CA - bluestone (GTS)
United States
But.. when using (.+?) 'Lazy', the expression (stops?) once it finds the first occurrence: So after the first </h3>, the system finds simply:
United States
Since the first condition is met, it doesn't matter what is in the second (otherwise) match of the pattern, as the expresison is now lazy and only finds the first occurrence.
Do I got this right?
To put it in another example (not using here document or HTML tags):
$str = 'there\'s no place like home, as there\'s only one place to call home.';
preg_match('#there\'s(.+)home#', $str, $match);
foreach($match as $val){
echo $val . '<br />';
}
ouputs (as an array with two keys / values):
there's no place like home, as there's only one place to call home <-- this is $match[0]
no place like home, as there's only one place to call <-- this is $match[1]
And this is because of the greedy nature (lack of the question mark character), it starts from the first "there's" and matches up to the second "home" and thus includes everything inbetween.
But with the (.+?) in use:
preg_match('#there\'s(.+?)home#', $str, $match);
I get:
there's no place like home <-- this is $match[0]
no place like <-- this is $match[1]
Since it is lazy, it only matches the first occurrence between "there's" and "home" (the first home that is).
On a side note, I didn't realise that you can match a section of characters doing it this way ($match[1]). Prior to this post, I would have thought that one would need to use positive look behind assertions and positive look ahead assertions to exclude the words "there's" and "home".. but as it turns out, due the (.+?) being in parenthesis, this match is put into another key.
This is an eye opener.. makes me see things a little differently now. Hope I got all this right.
Cheers,
NRG