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 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line