douguachi0056 2013-09-26 07:29
浏览 59
已采纳

PHP:如何检查<p>是否具有DOMDocument的<iframe>子节点?

How can I check if a p has a child node of iframe with DOMDocument?

For instance,

<p><iframe ....></p>

I want to print this only,

<iframe ....>

While,

<p>bla bla bal</p>

then do nothing or just print whatever inside the p,

<p>bla bla bal</p>

Or,

<p>bla bla <b>bal</b></p>

then do nothing or just print whatever inside the p,

<p>bla bla <b>bal</b></p>

my php,

$dom = new DOMDocument;
$dom->loadHTML($item_html);

if($dom->getElementsByTagName('p')->length > 1 )
{
    ...
}
else // if it is only a single paragraph... then do what I want above...
{

    foreach ($dom->getElementsByTagName('p') as $node)
    {
        if ($node->hasChildNodes()) 
        {
           foreach( $dom->getElementsByTagName('iframe') as $iframe ) 
            {
               ... something
            }
        }
        else
        {
            ... 
        }
     }
}

is it possible?

  • 写回答

2条回答 默认 最新

  • duanposhi3641 2013-09-27 17:24
    关注

    You're trying to find all iframe elements that are the only childnodes of the p elements.

    If found you want to replace their parent p element with them.

    /** @var DOMElement $p */
    foreach ($doc->getElementsByTagName('p') as $p) {
        if ($p->childNodes->length !== 1) {
            continue;
        }
    
        $child = $p->childNodes->item(0);
    
        if (! $child instanceof DOMElement) {
            continue;
        }
    
        if ($child->tagName !== 'iframe') {
            continue;
        }
    
        $p->parentNode->insertBefore($child, $p);
        $p->parentNode->removeChild($p);
    }
    

    This foreach loop just iterates over all p elements, ignores all that don't have a single child node that is not a DOMElement with the iframe tagname (note: always lowercase in the compare).

    If one p element is found, then the inner iframe is moved before it and then the paragraph is removed.

    Usage Example:

    <?php
    /**
     * @link http://stackoverflow.com/q/19021983/367456
     */
    
    $html = '
    <p><iframe src="...."></p>
    <p>bla bla bal</p>
    <p>bla bla <b>bal</b></p>
    <p></p>
    ';
    
    $doc = new DOMDocument();
    $doc->loadHTML($html);
    
    /** @var DOMElement[] $ps */
    // $ps = $;
    
    /** @var DOMElement $p */
    foreach ($doc->getElementsByTagName('p') as $p) {
        if ($p->childNodes->length !== 1) {
            continue;
        }
    
        $child = $p->childNodes->item(0);
    
        if (!$child instanceof DOMElement) {
            continue;
        }
    
        if ($child->tagName !== 'iframe') {
            continue;
        }
    
        $p->parentNode->insertBefore($child, $p);
        $p->parentNode->removeChild($p);
    }
    
    // output
    foreach ($doc->getElementsByTagName('body')->item(0)->childNodes as $child) {
        echo $doc->saveHTML($child);
    }
    

    Demo and Output:

    <iframe src="...."></iframe>
    <p>bla bla bal</p>
    <p>bla bla <b>bal</b></p>
    <p></p>
    

    Hope this is helpful.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64