Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 试图删除HTML中的脚本标记_Php_Javascript_Html_Dom_Tags - Fatal编程技术网

Php 试图删除HTML中的脚本标记

Php 试图删除HTML中的脚本标记,php,javascript,html,dom,tags,Php,Javascript,Html,Dom,Tags,我正在尝试使用PHP从HTML中删除脚本标记,但如果javascript中有HTML,则无法工作 例如,如果脚本标记包含以下内容: function tip(content) { $('<div id="tip">' + content + '</div>').css $text = file_get_content('index.html'); while(mb_strpos($text, '<script') != false) { $star

我正在尝试使用PHP从HTML中删除脚本标记,但如果javascript中有HTML,则无法工作

例如,如果脚本标记包含以下内容:

function tip(content) {
        $('<div id="tip">' + content + '</div>').css
$text = file_get_content('index.html');
while(mb_strpos($text, '<script') != false) {
$startPosition = mb_strpos($text, '<script');
$endPosition = mb_strpos($text, '</script>');
$text = mb_substr($text, 0, $startPosition).mb_substr($text, $endPosition + 7, mb_strlen($text));
}
echo $text;

一些基于正则表达式的预处理怎么样

示例
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>

我的例子
试验
document.getElementById('foo').innerHTML='Hello World!';
脚本标记删除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";

?>

您可以使用函数。您可以在其中允许您想要允许的
HTML
属性。

我认为这是“此时此地”的问题,您不需要什么特殊的东西。就这样做吧:

function tip(content) {
        $('<div id="tip">' + content + '</div>').css
$text = file_get_content('index.html');
while(mb_strpos($text, '<script') != false) {
$startPosition = mb_strpos($text, '<script');
$endPosition = mb_strpos($text, '</script>');
$text = mb_substr($text, 0, $startPosition).mb_substr($text, $endPosition + 7, mb_strlen($text));
}
echo $text;
$text=file\u get\u content('index.html');

而(mb)($text,'请注意,PHP是一种服务器端语言,因此,如果您试图在页面加载后删除脚本标记,它不会做任何事情。您使用的是DOMDocument吗?这是javascript模板吗?如果是,您不能用DOMDocument执行此操作。请尝试“是”,我使用的是DOMDocument,所有操作都非常完美,但奇怪的是,它仍然在
@Alex你能提供一些详细信息吗?对我来说,它还删除了所有包含div的脚本标记。我做了一些编辑,现在可以工作了。谢谢你的帮助。:)