从 Postgress 正则表达式替换 PHP 语言中的匹配


From Postgress regexp replace match in PHP language

我需要一些帮助。

我有PostgreSQL regexp_replace模式,比如:

 regexp_replace(lower(institution_title),'[[:cntrl:]]|[[[:digit:]]|[[:punct:]]|[[:blank:]]|[[:space:]|„|“|“|”"]','','g')

我需要PHP语言中的这个替代方案

因为一半来自 postgress db,我还必须比较来自 php 的字符串。

您可以将相同的 POSIX 字符类与 PHP PCRE 正则表达式一起使用:

preg_replace('/[[:cntrl:][:digit:][:punct:][:blank:][:space:]„““”"]+/', '', strtolower($institution_title))

查看演示

此外,PCRE 中还有 Unicode 类别类。因此,您也可以尝试

preg_replace('/['p{Cc}'d'p{P}'s„““”"]+/u', '', mb_strtolower($institution_title, 'UTF-8'))

其中'p{Cc}代表控制字符,'d代表数字,'p{P}代表标点符号,'s代表空格。

我也在添加/u修饰符来处理 Unicode 字符串。

查看正则表达式演示

汉克斯伙计

们,但我遇到了另一个问题,如果有特定的符号,我无法匹配字符串,

这是我的Postgres SQL输出:

.SQL:

select regexp_replace(lower(title),'[[:cntrl:]]|[[[:digit:]]|[[:punct:]]|[[:blank:]]|[[:space:]|„|“|“|”"]','','g')
from cls_institutions

输出:

"oxforduniversity"
"šiauliųuniversitetas"
"harwarduniversity"
"internationalbusinessschool"
"vilniuscollege"
"žemaitijoskolegija"
"worldhealthorganization"

但是在 PHP 中输出有点不同:我得到了我的数组与机构:

$institutions[] = "'".preg_replace('/[[:cntrl:][:digit:][:punct:][:blank:][:space:]„““”"]+/', '', strtolower($data[0]))."'";

PHP 输出如下:

"oxforduniversity",
"Šiauliųuniversitetas",
"harwarduniversity",
"internationalbusinessschool",
"vilniuscollege",
"Žemaitijoskolegija",
"worldhealthorganization"

第一个字母不是降低大小写,不知何故...我错过了什么?