如何忽略span标签dom html

问题描述 投票:-3回答:1

嗨,我想在这段代码中刮掉Brand New Apple iPhone 8 64GB or 256GB - Sealed - GSM Unlocked,但它也刮掉了它,我怎么忽略跨度文本。

<h1 class="it-ttl" itemprop="name" id="itemTitle"><span class="g-hdn">Details about  &nbsp;</span>Brand New Apple iPhone 8 64GB or 256GB - Sealed - GSM Unlocked</h1>

这是代码:

$productname = $html->find("h1[class='it-ttl']",0)->plaintext;

echo $productname;
php dom
1个回答
-1
投票

strip_tags_content是一个用PHP Strip Tags编写的函数,函数的所有者用这些单词解释。您可以在链接中找到更多示例。

输出为:全新Apple iPhone 8 64GB或256GB - 密封 - GSM解锁

“嗨。我创建了一个删除HTML标签及其内容的功能”

 function strip_tags_content($text, $tags = '', $invert = FALSE) {

        preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags);
        $tags = array_unique($tags[1]);

        if(is_array($tags) AND count($tags) > 0) {
            if($invert == FALSE) {
                return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text);
            }
            else {
                return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text);
            }
        }
        elseif($invert == FALSE) {
            return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text);
        }
        return $text;
    }


    $string = '<h1 class="it-ttl" itemprop="name" id="itemTitle"><span class="g-hdn">Details about  &nbsp;</span>Brand New Apple iPhone 8 64GB or 256GB - Sealed - GSM Unlocked</h1>';
    $string = strip_tags_content($string,'<span>',true);
    $string = strip_tags($string);

    echo $string;

对于定义此函数后的问题,请调用

$productname = $html->find("h1[class='it-ttl']",0)->plaintext; 
$productname = strip_tags_content($productname ,'<span>',true); 
$productname = strip_tags($string);
© www.soinside.com 2019 - 2024. All rights reserved.