简体   繁体   中英

PHP preg_match_all html tags content search

I have a problem, i want to do count of symbols in html tags in text.

Text example 1:

Hello <b>world</b>, <i>stackoverflow</i>

Text example 2:

Hello <b>world, <i>stackoverflow</i></b>

So, I need to count how many symbols in b and in i block separately.

I did this:

preg_match_all('#<(b|i)>(.*)<\/(b)>#Uusi', $temp, $tags_check);

foreach($tags_check[2] as $val)
{
    if(mb_strlen($val) > 50)
    {
        $errors = 'error';
        break;
    }
}

But it`s works only for first example, in second example i need to do something with regexp. I need to search on start b and on end b , but not on start b and on end i , how can i do this?

DOM + XPath way to accomplish that:

$html = 'Hello <b>world</b>, <i>stackoverflow</i>';
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$error_nodes = $xpath->query('//b[string-length(text()) > 50]|//i[string-length(text()) > 50]');

foreach ($error_nodes as $node) {
    print $node->nodeValue;
}

Good luck!

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