Regular Expression (Regex) for HTML parsing in PHP

Possible Duplicate:
RegEx match open tags except XHTML self-contained tags

I'm stuck in weird regex problem, I'm parsing some HTML table in PHP.

RegEx I'm using : <td[^>]*>(h.*?)</td>

<td>other data</td> <td>other data</td><td>Data_needed</td> <td>--</td>

but its matching all other data too.

Now I want to match it to <td>Data_needed</td> <td>--</td>

I tried some regular expressions which gives output like

other data</td> <td>other data</td><td>Data_needed</td> <td>--

starting from first <td> to last </td>

but I want Data_needed from <td>Data_needed</td> <td>--</td>


Do not use regex for parsing HTML or XML (including XHTML). Ever.

Use an HTML or XML parser instead. A quick search for "php html parsing" turned up this tool, Simple HTML DOM, as the first hit. PHP also has DOM and SAX tools built in.


You can use the Simple HTML DOM for that instead.

A HTML DOM parser written in PHP5+ let you manipulate HTML in a very easy way!


一般的HTML解析不应该使用正则表达式来完成,但是如果你的HTML很简单并且没有嵌套,你可以尝试

.*<td[^>]*>(.*?)</td>s*<td>--</td>
链接地址: http://www.djcxy.com/p/76860.html

上一篇: 构建正则表达式(RegEx)以提取HTML标记的文本

下一篇: 正则表达式(正则表达式)用于PHP中的HTML解析