tags:

views:

54

answers:

2
<tr  id='ieconn3' >
  <td><table width='100%'><tr><td valign='top'><table width='100%'><tr><td>aaaaa
<br>&nbsp;</td></tr><tr><td> 

I want to get the aaaaa part till <br> or </td>. I tried lots of patterns but didnt work. any help?

A: 

You shouldn’t try to use regular expressions to parse HTML as HTML is not a regular language and thus cannot be described with regular expressions. Use a proper HTML parser instead.

If you’re using XHTML, you can use SimpleXML to parse it as XML and query it with SimpleXMLElement::xpath. And for HTML documents, you can use the Simple HTML DOM Parser. And DOMDocument can even handle both XHTML and HTML.

Gumbo
I am not gonns parse html I am just getting content in tags
kk
@kk: But you need to parse the HTML code to get the proper contents.
Gumbo
<tr id='ieconn2' > <td><table width='100%'><tr><td valign='top'><table width='100%'><tr><td>THIS PART ONLY<br>I want to get THIS PART ONLY but it gives me 1 child node. and with out any html tags I cant preg_replace?
kk
A: 

As Gumbo pointed out, this will only result in a giant mess if you insist on using a regexp for this. However, if you are sure the HTML does not chance, this one will do the trick:

/<tr><td>(.*)<\/td><\/tr>/

use like this:

$string = "<tr  id='ieconn3' >
<td><table width='100%'><tr><td valign='top'><table width='100%'><tr><td>aaaaa<br>&nbsp;</td></tr><tr><td>";

$matches = array();
preg_match("/<tr><td>(.*)<\\/td><\\/tr>/", $string, $matches);

print($matches[1]);
Rodin