PHP preg_replace特殊字符 [英] PHP preg_replace special characters
问题描述
我想用下划线(_)
替换所有非字母和数字字符,例如/&%#$
等,并用""空白(所以没有下划线)替换所有
'
(单引号).>
因此,"没有任何"(忽略双引号)将变为" There_wouldnt_be_any ".
我对reg表达式毫无用处,因此在帖子中.
欢呼
$newstr = preg_replace('/[^a-zA-Z0-9\']/', '_', "There wouldn't be any");
$newstr = str_replace("'", '', $newstr);
我将它们放在两行中,以使代码更加清晰.
注意:如果您正在寻找Unicode支持,请参阅下面的Filip答案.除了A-z
以外,它将匹配所有注册为字母的字符.
I am wanting to replace all non letter and number characters i.e. /&%#$
etc with an underscore (_)
and replace all '
(single quotes) with ""blank (so no underscore).
So "There wouldn't be any" (ignore the double quotes) would become "There_wouldnt_be_any".
I am useless at reg expressions hence the post.
Cheers
$newstr = preg_replace('/[^a-zA-Z0-9\']/', '_', "There wouldn't be any");
$newstr = str_replace("'", '', $newstr);
I put them on two separate lines to make the code a little more clear.
Note: If you're looking for Unicode support, see Filip's answer below. It will match all characters that register as letters in addition to A-z
.
这篇关于PHP preg_replace特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!