问题描述
preg_replace
当我在从数据库获取的字符串上使用它时不会返回所需的结果.
preg_replace
does not return desired result when I use it on string fetched from database.
$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");
我在这里遗漏了什么?
推荐答案
事实证明您的字符串中有 Unicode 破折号.要匹配所有 Unicode 破折号,请使用
It turns out you have Unicode dashes in your strings. To match all Unicode dashes, use
/[\p{Pd}\xAD]/u
查看正则表达式演示
\p{Pd}
匹配 Unicode Character Category 'Punctuation, Dash' 但是软连字符 \xAD
,因此它应该与 \p 结合{Pd}
在字符类中.
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.
/u
修饰符使模式识别 Unicode,并使正则表达式引擎将输入字符串视为 Unicode 代码点序列,而不是字节序列.
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.
这篇关于PHP正则表达式不适用于数据库中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!