[英]PHP regular expression not working with string from database
preg_replace
does not return desired result when I use it on string fetched from database.当我在从数据库获取的字符串上使用
preg_replace
它不会返回所需的结果。
$result = DB::connection("connection")->select("my query");
foreach($result as $row){
//prints run-d.m.c.
print($row->artist . "\n");
//should print run.d.m.c
//prints run-d.m.c
print(preg_replace("/-/", ".", $row->artist) . "\n");
}
This occurs only when i try to replace -
(dash).仅当我尝试替换
-
(破折号)时才会发生这种情况。 I can replace any other character.我可以替换任何其他字符。 However if I try this regex on simple string it works as expected:
但是,如果我在简单的字符串上尝试这个正则表达式,它会按预期工作:
$str = "run-d.m.c";
//prints run.d.m.c
print(preg_replace("/-/", ".", $str) . "\n");
What am I missing here?我在这里缺少什么?
It turns out you have Unicode dashes in your strings.事实证明,您的字符串中有 Unicode 破折号。 To match all Unicode dashes, use
要匹配所有 Unicode 破折号,请使用
/[\p{Pd}\xAD]/u
See the regex demo查看正则表达式演示
The \\p{Pd}
matches any hyphen in the Unicode Character Category 'Punctuation, Dash' but a soft hyphen, \\xAD
, hence it should be combined with \\p{Pd}
in a character class. \\p{Pd}
匹配Unicode 字符类别“标点符号,破折号”中的任何连字符,但软连字符\\xAD
,因此它应该与字符类中的\\p{Pd}
组合。
The /u
modifier makes the pattern Unicode aware and makes the regex engine treat the input string as Unicode code point sequence, not a byte sequence. /u
修饰符使模式识别 Unicode,并使正则表达式引擎将输入字符串视为 Unicode 代码点序列,而不是字节序列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.