如何在PHP中删除字符串中的JavaScript残留
我正在运行以下步骤,试图清理使用$query\u text\u lower=file\u get\u contents(websiteURL)获得的字符串 我想要的,就是回敬你的话。没有javascript,没有随机数,没有CSS或任何其他类型的脚本如何在PHP中删除字符串中的JavaScript残留,php,string,preg-replace,Php,String,Preg Replace,我正在运行以下步骤,试图清理使用$query\u text\u lower=file\u get\u contents(websiteURL)获得的字符串 我想要的,就是回敬你的话。没有javascript,没有随机数,没有CSS或任何其他类型的脚本 //remove javascript $query_text_lower = preg_replace("/<script[^>]*>.*?< *script[^>]*>/i", "", $new_text);
//remove javascript
$query_text_lower = preg_replace("/<script[^>]*>.*?< *script[^>]*>/i", "", $new_text);
//remove html tags
$query_text_lower2 = strip_tags($query_text_lower);
//removes any text containing links (may not be best, as some sites link useful words within the text. Does tend to remove a lot of ads though
$query_text_lower3 = preg_replace('/<a\s.*?>.*?<\/a>/s', '', $query_text_lower2);
//removes linebreaks
$query_text_lower4 = trim(preg_replace('/\s+/', ' ', $query_text_lower3));
echo $query_text_lower4;
die();
您的javascript正则表达式已关闭 你有:
$query_text_lower = preg_replace("/<script[^>]*>.*?< *script[^>]*>/i", "", $new_text);
$query\u text\u lower=preg\u replace(“/]*>.*?”<*script[^>]*>/i“,”,$new\u text);
您没有在返回的文档中进行检测,因此它没有从页面中删除javascript代码本身,但是当您调用striptags时,您正在删除标记,因此它们不会出现在最终输出中。然而,我看不到你的网站,所以我不能百分之百的关注这个
让我知道这是否有意义。基本上,在我看来,你的第一个正则表达式实际上并不匹配任何东西。你不能用正则表达式解析这些东西。建议使用现有的DOM工具解析HTML是正确的方法。考虑或遍历DOM并获取所需的内部文本。可能是重复的是的,你可以非常努力地尝试,但你不会获得所有内容。UpvoteI将使用DOM遍历工具进行探索-谢谢。我想你已经指出了问题所在。将正则表达式更改为:$query\u text\u lower=preg\u replace('/]*>(.*?)/is','',$new\u text);它现在起作用了
$query_text_lower = preg_replace("/<script[^>]*>.*?< *script[^>]*>/i", "", $new_text);