0

Here is my code:

<?php
$text='<td valign="top">text</td><td>more text</td>';
preg_match('/<td valign="?top"?>.*<\/td>)/s', $text, $matches);

?>

$matches[0] should return<td valign="top">text</td> but it returns <td valign="top">text</td><td>more text</td>. Why?

blake305
  • 2,196
  • 3
  • 23
  • 52
  • 1
    http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags –  Aug 09 '11 at 23:48
  • possible duplicate of [Regex for bbcode seems to fail on long sentences](http://stackoverflow.com/questions/7004336/regex-for-bbcode-seems-to-fail-on-long-sentences), see http://stackoverflow.com/questions/7004336/regex-for-bbcode-seems-to-fail-on-long-sentences/7004356#7004356 for the answer. – hakre Aug 09 '11 at 23:48

3 Answers3

4

Make the .* pattern un-greedy by adding a ?:

preg_match('/<td valign="?top"?>.*?<\/td>)/s', $text, $matches);
                                ^^^ 

Here is an example of greedy vs. non-greedy regex: http://www.exampledepot.com/egs/java.util.regex/Greedy.html

Why?

That is how regex work. You might be looking for an XML Parser instead:

$text='<td valign="top">text</td><td>more text</td>';

$xml = simplexml_load_string("<x>$text</x>");
list($td) = $xml->xpath('td[@valign="top"]');
echo $td->asXML(); # <td valign="top">text</td>
hakre
  • 193,403
  • 52
  • 435
  • 836
0

? cannot stand alone. It's a modifier and greedy operator. It means looking for the closest match. Maybe you need a .+? in you expression.

Micromega
  • 12,486
  • 7
  • 35
  • 72
0

try this <td valign.+?\/td> I think it works

theHack
  • 1,938
  • 9
  • 26
  • 33