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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog