【问题标题】:Is there any script like ckeditor that removes style and script when we paste code in textarea? [closed]当我们将代码粘贴到 textarea 中时,是否有像 ckeditor 这样的脚本会删除样式和脚本? [关闭]
【发布时间】:2016-01-11 03:10:03
【问题描述】:

有没有像 ckeditor 这样在我们将代码粘贴到文本区域时删除 css 和脚本的脚本?当用户从其他地方复制文本并将其粘贴到我的网站时,我想删除 css 和脚本

【问题讨论】:

  • 您只想删除 css 和 javascript?或者所有的html标签
  • 只有css和像ckeditor这样的javascript是一个例子
  • 没有内置函数让我给你写代码。

标签: javascript php jquery css ckeditor


【解决方案1】:
$myHtml = // here is your html;
$doc = new DOMDocument();
$doc->loadHTML($myHtml);

removeElementsByTagName('script', $doc);
removeElementsByTagName('style', $doc);

$html = $doc->saveHtml();

function removeElementsByTagName($tagName, $document) {
  $nodeList = $document->getElementsByTagName($tagName);
  for ($nodeIdx = $nodeList->length; --$nodeIdx >= 0; ) {
    $node = $nodeList->item($nodeIdx);
    $node->parentNode->removeChild($node);
  }
}

(代码来自:https://stackoverflow.com/a/20082963/610573

【讨论】:

  • 我将在哪个变量中得到最终的 o/p
  • 您将在$html获得最终结果
【解决方案2】:
猜你喜欢
  • 1970-01-01
  • 2012-11-06
  • 1970-01-01
  • 1970-01-01
  • 2014-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多