简体   繁体   中英

php preg_match pattern problem,regular expression pattern

<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?

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.

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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM