dongzhoutuo0883 2015-03-25 16:06
浏览 69
已采纳

在HTML中查找iFrame并检查其SRC

I have a website where a user can among other objects like text and images also insert a YouTube video into CKEditor type textarea form.

YouTube video is embedded by iFrame objects. But I don't want users to be able to insert any other iFrame except for YouTube (I am sure you can guess why)

So when the form is submitted I want to scan the $text variable for all iFrames and if they do not point to youtube.com or youtube-nocookie.com, remove those iFrame tags.

These are iFrames with allowed sources:

<iframe allowfullscreen="" frameborder="0" height="360" src="//www.youtube.com/embed/6dk-5HN4fvg" width="640"></iframe>

<iframe allowfullscreen="" frameborder="0" height="360" src="//www.youtube-nocookie.com/embed/IY37l4PDsao" width="640"></iframe>

The task:

  1. find the iFrame
  2. find the value of its SRC
  3. check if it is an allowed domain
  4. if not delete it, or disable it, but preserve the rest of the surrounding HTML
  5. check if there is another
  • 写回答

1条回答 默认 最新

  • dpqjvoq9033 2015-03-25 16:23
    关注

    Here is one way of utilizing DOM and XPath to achieve this task.

    $doc = new DOMDocument;
    
    @$doc->loadHTML($html); 
    $doc->removeChild($doc->doctype);
    
    $xp  = new DOMXPath($doc);
    $tag = $xp->query("//iframe[not(contains(@src, 'youtube.com') or 
                                    contains(@src, 'youtube-nocookie.com'))]");
    
    foreach ($tag as $t) {
       $t->parentNode->removeChild($t);
    }
    
    echo $doc->saveHTML();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号