[英]PHP regex remove non alphanumeric except period
I'm having trouble finding a solution to this. 我在寻找解决方案时遇到了麻烦。 How can I avoid losing the period in this regex?
如何避免在此正则表达式中丢失期限?
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
$text = preg_replace('@[^0-9a-z\.]+@i', '-', $text);
以区分大小写的方式替换非0-9,az或句点的任何内容。
Just add the dot to your character class: 只需将点添加到您的角色类中:
$text = preg_replace('~[^\\pL\d.]+~u', '-', $text);
You are using a negated character class (the [^
part) so anything that does not match any of the characters in that character class, gets replaced. 您正在使用否定的字符类(
[^
部分),以便替换与该字符类中的任何字符都不匹配的所有字符。
By the way, your question title does not match your regex. 顺便说一句,您的问题标题与您的正则表达式不匹配。
What the heck is "\\\\pL"? 什么是“ \\\\ pL”? AFAIK this matches a Backslash and the letters p and L.
AFAIK这与反斜杠以及字母p和L匹配。
Is this what you mean? 你是这个意思吗?
<?php
echo preg_replace('/[^a-z0-9.]+/ui', '-', 'abc093.-23.-2ªıØẞÆ.23.OAIFJ→øæł¶iwoeweo');
?>
Result: abc093.-23.-2-.23.OAIFJ-iwoeweo 结果:abc093.-23.-2-.23.OAIFJ-iwoeweo
不要进行两次转义,并且要完全与Unicode兼容,数字是: \\pN
然后:
$text = preg_replace('~[^\pL\pN]+~u', '-', $text);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.