Search code examples
phpregexpostgresqlregexp-replace

From Postgress regexp replace match in PHP language


i need some help.

I have PostgreSQL regexp_replace pattern, like:

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

and i need this one alternative in PHP language

Because one half is from postgress db, and i have to compare strings from php aswell.


Solution

  • You may use the same POSIX character classes with PHP PCRE regex:

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

    See demo

    Besides, there are Unicode category classes in PCRE. Thus, you may also try

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

    Where \p{Cc} stands for Control characters, \d for digits, \p{P} for punctuation, and \s for whitespace.

    I am adding /u modifier to handle Unicode strings, too.

    See a regex demo