0

Hello I've studying regex and I want know why:

This code:

<?PHP
$str = '<TD class=""><option value="123">abc</option>
</TD>
        <TD class=""><option value="123">abcwda</option>
        </TD>';

preg_match_all('#<TD([\S\s]+)</TD>#s', $str, $tabela);
?>

Return:

Array
(
    [0] =>  class=""><option value="123">abc</option>
</TD>
        <TD class=""><option value="123">abcwda</option>

)

And why does it not return:

Array
(
    [0] =>  class=""><option value="123">abc</option>
    [1] =>  class=""><option value="123">abcwda</option>

)

?

Andreas
  • 23,610
  • 6
  • 30
  • 62
zupovawu
  • 3
  • 2

1 Answers1

0

Because of greedy +. Use its lazy counterpart (+?):

preg_match_all('#<TD(.+?)</TD>#s', $str, $tabela);

Please also note that since you're using the /s modifier, . will match newline characters so you don't need the [\S\s] construct.

Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40