php对字符串中的特殊符号进行过滤的方法

1、使用htmlspecialchars函数:此函数将特殊字符转换为对应的HTML实体。示例代码如下:

php 复制代码
$str = "<script>alert('XSS')</script>";
$filtered_str = htmlspecialchars($str);
echo $filtered_str;

输出:

&lt;script&gt;alert(&#039;XSS&#039;)&lt;/script&gt;

2 、使用strip_tags函数:此函数可以删除字符串中的HTML和PHP标签。示例代码如下:

php 复制代码
$str = "<p>这是一个段落<em>带有强调标签</em></p>";
$filtered_str = strip_tags($str);
echo $filtered_str;

3、使用preg_replace函数:可以使用正则表达式来替换字符串中的特殊字符。示例代码如下:

php 复制代码
$str = "Hello, @world!";
$filtered_str = preg_replace('/[^a-zA-Z0-9]/', '', $str);
echo $filtered_str;
相关推荐
小小鱼儿飞1 小时前
QT音乐播放器18----新歌速递播放、隐藏顶部和底部工具栏、自定义ToolTips
开发语言·qt
穆雄雄1 小时前
Rust 程序适配 OpenHarmony 实践:以 sd 工具为例
开发语言·rust·harmonyos
0***141 小时前
Swift资源
开发语言·ios·swift
z***I3941 小时前
Swift Tips
开发语言·ios·swift
J***Q2921 小时前
Swift Solutions
开发语言·ios·swift
铅笔小新z1 小时前
C++入门指南:开启你的编程之旅
开发语言·c++
Gavin-Wang1 小时前
Swift + CADisplayLink 弱引用代理(Proxy 模式) 里的陷阱
开发语言·ios·swift
星星20253 小时前
VSCode插件精选:4款高效UML绘图工具
笔记
molunnnn6 小时前
第四章 Agent的几种经典范式
开发语言·python