0

PHP を使用して HTML からスクリプト タグを削除しようとしていますが、javascript 内に HTML があると機能しません。

たとえば、スクリプト タグに次のようなものが含まれているとします。

function tip(content) {
        $('<div id="tip">' + content + '</div>').css

で停止し</div>、残りのスクリプトは引き続き考慮されます。

これは、スクリプトタグを削除するために使用しているものです。

foreach ($doc->getElementsByTagName('script') as $node)
{
    $node->parentNode->removeChild($node);
}
4

3 に答える 3

0

機能を使用できstrip_tagsます。HTML許可したい属性を許可することができます。

于 2015-08-03T07:04:47.830 に答える
0

正規表現ベースの前処理はどうですか?

input.html

<html>
  <head>
    <title>My example</title>
  </head>
  <body>
    <h1>Test</h1>
    <div id="foo">&nbsp;</div>
    <script type="text/javascript">
      document.getElementById('foo').innerHTML = '<span style="color:red;">Hello World!</span>';
    </script>
  </body>
</html>

phpスクリプトを削除するスクリプトタグ:

<?php

    // unformatted source output:
    header("Content-Type: text/plain");

    // read the example input file given above into a string:
    $input = file_get_contents('input.html');

    echo "Before:\r\n";
    echo $input;
    echo "\r\n\r\n-----------------------\r\n\r\n";

    // replace script tags including their contents by ""
    $output = preg_replace("~<script[^<>]*>.*</script>~Uis", "", $input);

    echo "After:\r\n";
    echo $output;
    echo "\r\n\r\n-----------------------\r\n\r\n";

?>
于 2013-02-09T22:53:28.680 に答える