简体   繁体   中英

preg_match between html tags

I need to grab the names between the html tags.

<div class="from"><span class="profile fn">firstnamed familyname</span></div>

so far I tried according to examples from other poeple with the same question:

preg_match(";from"><span class="profile fn>(.?)</span></div>;", $text, $match)

but it doesn't work.

What is the correct way?

Thanks a lot.

preg_match(";from"><span class="profile fn>(.?)</span></div>;", $text, $match)

... should trigger this:

Parse error: syntax error, unexpected '<'

Apart from that:

  • You seek for an unclosed attribute that's not in the original text:

    class="profile fn vs class="profile fn"

  • You seek for zero or one characters:

    .?

Fixed regexp would be:

$text = '<div class="from"><span class="profile fn">firstnamed familyname</span></div>';
preg_match(';from"><span class="profile fn">(.*)</span></div>;', $text, $match);
var_dump($match);

Of course, this will probably break on large HTML documents (as soon as there's another </span></div> bit later on). Regular expressions are impossible to get right when used for parsing HTML.

This:

preg_match(";from"><span class="profile fn>(.?)</span></div>;", $text, $match)

is syntactically incorrect, you have to escape the double quotes:

preg_match(";from\"><span class=\"profile fn>(.?)</span></div>;", $text, $match)

您需要转义特殊字符(例如引号):

preg_match(";from\"><span class\=\"profile fn>(.?)</span></div>;", $text, $match)

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