开发者

cannot catch the html tags using regex ! [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How to parse HTML with PHP?

crawling a html page using php?

Im trying to find a way to find the html tags.

So i tried to use preg_match_all function to find the html tags.

and here is the code what i used :

$code = "<div>This is a test</div>";
preg_match_all("/(<[^<>]+>)([^<>]+)(<[^<>]+>)/",
$code, $matches);
var_dump($matches);

whe开发者_JAVA技巧n i used this code, and i try to run it.. the page returned

array(4) { [0]=> array(1) { [0]=> string(25) " This is a test " } [1]=> array(1) { [0]=> string(5) " " } [2]=> array(1) { [0]=> string(14) "This is a test" } [3]=> array(1) { [0]=> string(6) " " } }

as you see in the arrays.. the <div> and </div> didn't detected.

can you help me ? , and tell me where is the problem exactly.

Sorry for my english ..

Thanks,


Please see: RegEx match open tags except XHTML self-contained tags

As Bobince "explains", you shouldn't use regexes to parse HTML.

Since you're using PHP you can check out DOMDocument which allows you to safely parse HTML. Take a look at the reference material, attempt to incorporate DOMDocument into your app and if you still have problems answer a new question or appropriately edit this one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜