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?
Make the
.*pattern un-greedy by adding a?:Here is an example of greedy vs. non-greedy regex: http://www.exampledepot.com/egs/java.util.regex/Greedy.html
That is how regex work. You might be looking for an XML Parser instead: