js模拟点击a标签,触发href的两种方法——PHP实现过滤各种HTML标签
📅 2026-07-30 11:52 📂
<!DOCTYPE html>
<html><head>
<meta charset="utf-8">
<script src="https://libs.baidu.com/jquery/1.9.1/jquery.min.js#https://code.jquery.com/jquery-1.12.4.min.js" ></script></head>
<script type="text/javascript"> $(function(){ $("#sp").trigger("click"); }); </script>
<body> <a href="https://www.baidu.com?qq=1111111111111111111"><span id="sp"></span></a>
</body>
</html>
<!--本文件作测试用途,请勿删除,其他模板可直接套用 index 代码 修改指定参数,无需复制本文件内容 -->

 


 

代码如下:

$str = '郭碗瓢盆-<span style="color:#f00;">PHP</span>';

$str1 = strip_tags($str);          // 删除所有HTML标签

$str2 = strip_tags($str,'<span>'); // 保留 <span>标签

echo $str1; // 输出 郭碗瓢盆-PHP

echo $str2; // 样式不一样喔

例2、清除HTML标签字符串中某些属性代码

使用PHP处理从数据库中读取的文章HTML代码,然后用正则对代码进行匹配与修改,保存代码以后,用静态生成功能即可生成修改后的HTML页面了。

因为我们生成的代码是已经处理过的代码,所以就不会影响HTML页面的加载速度了。

具体 [示例代码]如下:

代码如下:

<?php

$str = "<img src="" width="245" height="138" onclick="window.open('xxxx.gif')" />";

$str = preg_replace('#onclick=([sS]*)"#','',$str);

// #符号前面有一个双引号要注意,代表结束的代码

print($str);

?>

首先分享一些比较常见的文章来源地址:https://www.yii666.com/blog/105614.html?action=onAll

$str=preg_replace("/<s*imgs+[^>]*?srcs*=s*('|")(.*?)\1[^>]*?/?s*>/i","", $str); //过滤img标签$str=preg_replace("/s+/","", $str); //过滤多余回车$str=preg_replace("/<[ ]+/si","<",$str); //过滤<__("<"号后面带空格)$str=preg_replace("/<!--.*?-->/si","",$str); //注释$str=preg_replace("/<(!.*?)>/si","",$str); //过滤DOCTYPE$str=preg_replace("/<(/?html.*?)>/si","",$str); //过滤html标签$str=preg_replace("/<(/?head.*?)>/si","",$str); //过滤head标签$str=preg_replace("/<(/?meta.*?)>/si","",$str); //过滤meta标签$str=preg_replace("/<(/?body.*?)>/si","",$str); //过滤body标签$str=preg_replace("/<(/?link.*?)>/si","",$str); //过滤link标签$str=preg_replace("/<(/?form.*?)>/si","",$str); //过滤form标签$str=preg_replace("/cookie/si","COOKIE",$str); //过滤COOKIE标签$str=preg_replace("/<(applet.*?)>(.*?)<(/applet.*?)>/si","",$str); //过滤applet标签$str=preg_replace("/<(/?applet.*?)>/si","",$str); //过滤applet标签$str=preg_replace("/<(style.*?)>(.*?)<(/style.*?)>/si","",$str); //过滤style标签$str=preg_replace("/<(/?style.*?)>/si","",$str); //过滤style标签$str=preg_replace("/<(title.*?)>(.*?)<(/title.*?)>/si","",$str); //过滤title标签$str=preg_replace("/<(/?title.*?)>/si","",$str); //过滤title标签$str=preg_replace("/<(object.*?)>(.*?)<(/object.*?)>/si","",$str); //过滤object标签$str=preg_replace("/<(/?objec.*?)>/si","",$str); //过滤object标签$str=preg_replace("/<(noframes.*?)>(.*?)<(/noframes.*?)>/si","",$str); //过滤noframes标签$str=preg_replace("/<(/?noframes.*?)>/si","",$str); //过滤noframes标签$str=preg_replace("/<(i?frame.*?)>(.*?)<(/i?frame.*?)>/si","",$str); //过滤frame标签$str=preg_replace("/<(/?i?frame.*?)>/si","",$str); //过滤frame标签$str=preg_replace("/<(script.*?)>(.*?)<(/script.*?)>/si","",$str); //过滤script标签$str=preg_replace("/<(/?script.*?)>/si","",$str); //过滤script标签$str=preg_replace("/javascript/si","Javascript",$str); //过滤script标签$str=preg_replace("/vbscript/si","Vbscript",$str); //过滤script标签$str=preg_replace("/on([a-z]+)s*=/si","On\1=",$str); //过滤script标签$str=preg_replace("/&#/si","&#",$str); //过滤script标签
⬅ 返回列表