本文介绍了删除PHP中的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有很多带有特殊字符的描述,例如é,ô等等,并尝试使用以下字符将其删除:
I have many descriptions with special characters like é, ô and many more and have tried to remove them with:
$string = str_replace("é", " ", $string)
$string = ereg_replace("é", " ", $string)
$string = mysql_real_escape_string($string)
但是没有任何效果,特殊字符仍然存在,并且描述未插入数据库中.我无法删除所有特殊字符,因为在说明中有需要的html标记.
But nothing works, the special characters are still there and the description is not inserted in the database. I can't remove all special characters because in the description there are html tags that are needed.
谢谢您的帮助!
推荐答案
简单易用:
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}
用法:
echo clean('a|"bc!@£de^&$f g');
将输出:abcdef-g
修改:
Hey, just a quick question, how can I prevent multiple hyphens from being next to each other? and have them replaced with just 1? Thanks in advance!
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
}
这篇关于删除PHP中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!