使用 php 删除特定的 html 标签

Abh*_*nyu 3 html php

因为我不想使用 php 的 stip_tags 函数而不是我想替换 <script> and </script>为空字符串,所以输出应该是 alert(1)。

输入:- <script>alert(1)</script>

输出:- 警报(1)

如何实现它。

kla*_*her 9

要么使用简单的替换:

$string = str_replace(array("<script>","</script>"), "");
Run Code Online (Sandbox Code Playgroud)

或正则表达式:

$string = preg_replace("/<script.*?>(.*)?<\/script>/im","$1",$string); 
Run Code Online (Sandbox Code Playgroud)

  • 请注意:正则表达式无法识别新行。添加 `s`-flag (`/ims`) 来解决这个问题。 (4认同)