Good day! I've got a small problem to solve, I couldn't get preg_match to work with multiple lines, here's the code: <?php $page = '<td class="top40txt3" align="left"><strong>Alejandro</strong></td> <td class="top40txt3" align="left">Lady GaGa</td>'; preg_match('!<td class="top40txt3" align="left"><strong>(.+?)</strong><td class="top40txt3" align="left">(.+?)</td>!sm',$page,$matches); print_r($matches); ?> PHP: I've tried all possible modifiers, but I always get zero output. Also tried quite a bit of Googling with no luck. Thanks for the time
<?php $page = '<td class="top40txt3" align="left"><strong>Alejandro</strong></td> <td class="top40txt3" align="left">Lady GaGa</td>'; preg_match('~<td class="top40txt3" align="left"><strong>(.+?)</strong></td>\s*<td class="top40txt3" align="left">(.+?)</td>~', $page, $matches); print_r($matches); ?> PHP:
Thank you atxsurf and danx10, problem is actually solved! I'm going to investigate what I did wrong myself.
You could also use the s modifier so the that the dot metacharacter in the pattern matches everything, including newlines.
I see, that worked as well, but, I'm a little confused about the "ungreedy" thing. I had a 248 patterns to match. .+? with modifier s works .+ with modifier s does not work (only matches the first pattern) \s+? with no modifier works \s+ with no modifier works Ungreedy pattern consumes as few characters as possible. Greedy pattern consumes as many characters as possible. It is because it is "greedy". - Source So, there has to be some sort of logic inside it...
The link you've given explains it pretty well. Here's my explanation: <?php // our string $string = "It's colder than a penguin's bollocks"; // non greedy will match everything up to the first apostrophe if (preg_match("/^(.*?)\'/", $string, $match)) echo 'Non-Greedy: ' . $match[1]; echo '<br />'; // greedy will match everything up to the last apostrophe if (preg_match("/^(.*)\'/", $string, $match)) echo 'Greedy: ' . $match[1]; ?> PHP: