javascript - PHP-Diff 跳过脚本标签
问题描述
我目前正在使用https://github.com/rashid2538/php-htmldiff来区分两个 html 页面,但问题是<script></script>
标签中的内容也有差异,我不想这样做,因为它破坏了可运行性。我查看了代码,但我不知道在哪里进行调整以忽略脚本标签,因为我只是开始使用 php 作为我的第一语言。我了解代码的解析原理,但其他一切对我来说都很神秘。
有人可以就改变什么提供建议或提示吗?
文件1:
<html>
<head>
<script> var thing = 'test'; </script>
</head>
<body>
</body>
</html>
文件2:
<html>
<head>
<script> var thing = 'anothertest'; </script>
</head>
<body>
</body>
</html>
结果:
<html>
<head>
<script> var thing = '<del class="diffmod">test</del><ins class="diffmod">anothertest</ins>'; </script>
</head>
<body>
</body>
</html>
解决方案
您可以使用regex
从两个html
字符串中查找脚本标签,然后使用preg_replace()删除它们。之后,将两个字符串与htmldiff
函数一起使用
$regex = '/<script\b[^>]*>(.*?)<\/script>/i';
$html1_parsed = preg_replace($regex, '', $html1);
$html2_parsed = preg_replace($regex, '', $html2);
推荐阅读
- amazon-web-services - ng 构建在 AWS 中出现“Javascript 堆内存不足”的 codeBuild 失败
- augmented-reality - Magic Leap Lumin 运行时编辑器无法在 macOS BigSur 上打开
- apache - 尝试通过 .htaccess 将页面从旧域重定向到新域
- sql - 如何在 SQL 中计算逗号分隔列表中的字符串项
- python-3.x - 使用 Python 多处理库和非守护进程嵌套进程,我如何检测父进程的停止/死亡?
- r - R中的动画直方图
- angular - 另一个 RXJS “ERROR TypeError: Object(...) is not a function”问题
- django - 使用登录的用户名作为 pk 进行 django 更新
- javascript - 创建一个在任何范围内更新变量的函数
- javascript - 尝试从 Vuex 模块调度操作不起作用。[vuex] 未知动作类型