PHP从字符串中删除特殊字符 [英] PHP remove special character from string
本文介绍了PHP从字符串中删除特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在删除特殊字符时遇到问题.我想删除除()/.%-&"以外的所有特殊字符,因为我将该字符串设置为标题.
I have problems with removing special characters. I want to remove all special characters except "( ) / . % - &", because I'm setting that string as a title.
我从原始代码中编辑了代码(如下所示):
I edited code from the original (look below):
preg_replace('/[^a-zA-Z0-9_ -%][().][\/]/s', '', $String);
但这无法删除特殊字符,例如:,","等.
But this is not working to remove special characters like: "’s, "“", "â€", among others.
原始代码:(这有效,但是会删除以下字符:()/.%-&)
original code: (this works but it removes these characters: "( ) / . % - &")
preg_replace('/[^a-zA-Z0-9_ -]/s', '', $String);
推荐答案
您的点匹配所有字符.像这样转义它(和其他特殊字符):
Your dot is matching all characters. Escape it (and the other special characters), like this:
preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $String);
这篇关于PHP从字符串中删除特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文