Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.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 如何通过正则表达式删除另一代码中的脚本标记_Php_Html_Regex - Fatal编程技术网

Php 如何通过正则表达式删除另一代码中的脚本标记

Php 如何通过正则表达式删除另一代码中的脚本标记,php,html,regex,Php,Html,Regex,我正在尝试使用正则表达式从源代码中删除脚本标记 /<\s*script[^>]*[^\/]>(.*?)<\s*\/\s*script\s*>/is /]*[^\/]>(.*)/is 但是当我需要删除另一个代码中的代码时,我遇到了这个问题 我在https://regex101.com/r/R6XaUT/1 如何正确创建正则表达式,使其能够覆盖所有代码?只需使用PHP函数strip\u标记即可。看 您还可以提供要保留的标记列表 == 另一种方法是: // Loa

我正在尝试使用正则表达式从源代码中删除脚本标记

/<\s*script[^>]*[^\/]>(.*?)<\s*\/\s*script\s*>/is
/]*[^\/]>(.*)/is
但是当我需要删除另一个代码中的代码时,我遇到了这个问题

我在
https://regex101.com/r/R6XaUT/1


如何正确创建正则表达式,使其能够覆盖所有代码?

只需使用PHP函数strip\u标记即可。看

您还可以提供要保留的标记列表

==

另一种方法是:

// Load a file into $html
$html = file_get_contents('scratch.html');
$matches = [];
preg_match_all("/<\/*([^\s>]*)>/", $html, $matches);

// Have a list of all Tags only once
$tags = array_unique($matches[1]);

// Find the script index and remove it
$scriptTagIndex = array_search("script", $tags);
if($scriptTagIndex !== false) unset($tags[$scriptTagIndex]);

// Taglist must be a string containing <tagname1><tagename2>...
$allowedTags = array_map(function ($s) { return "<$s>"; }, $tags);

// Stript the HTML and keep all Tags except for removed ones (script)
$noScript = strip_tags($html,join("", $allowedTags));

echo $noScript;
//将文件加载到$html中
$html=file_get_contents('scratch.html');
$matches=[];
preg_match_all(“/]*)>/”,$html,$matches);
//只有一次所有标签的列表
$tags=array_unique($matches[1]);
//找到脚本索引并将其删除
$scriptTagIndex=array_search(“脚本”,$tags);
如果($scriptTagIndex!==false)未设置($tags[$scriptTagIndex]);
//标记列表必须是包含。。。
$allowedTags=数组映射(函数($s){return”“;},$tags);
//剥离HTML并保留除已删除标记外的所有标记(脚本)
$noScript=strip_标记($html,join(“,$allowedTags));
echo$noScript;

示例文本:

$text = '<b>sample</b> text with <div>tags</div>'; 
Output: sample text with tags 
Output: text with 
Output: <b>sample</b> text with 
Output: text with <div>tags</div> 
带标签内容的结果($text):

$text = '<b>sample</b> text with <div>tags</div>'; 
Output: sample text with tags 
Output: text with 
Output: <b>sample</b> text with 
Output: text with <div>tags</div> 
带标签内容($text,”)的结果:

$text = '<b>sample</b> text with <div>tags</div>'; 
Output: sample text with tags 
Output: text with 
Output: <b>sample</b> text with 
Output: text with <div>tags</div> 
输出:带有
条带标签内容的结果($text',TRUE)

$text = '<b>sample</b> text with <div>tags</div>'; 
Output: sample text with tags 
Output: text with 
Output: <b>sample</b> text with 
Output: text with <div>tags</div> 
输出:带标记的文本
我希望有人有用:)

谢谢,但我只需要清除脚本标记,排除列表将非常大。是否还要删除脚本标记之间的内容?@AlexKovalev为什么只清除脚本标记?如果您关心的是安全性,那么您需要意识到您可以从html标记属性(如
onLoad
)运行javascript,因此只删除脚本标记不会带来任何好处。@jeroen这不是为了安全性,这段代码会阻止DOM解析器。除非清除脚本,否则类DOMDocumentС无法解析文档。@AlexKovalev真的吗?也许在这里您可以找到更好的解析器:。你可能想把它加到你的问题上,也许它会吸引到不同方向的答案