PHP regular expression not working with string from database

前端 未结 1 1623
悲&欢浪女
悲&欢浪女 2021-01-23 09:14

preg_replace does not return desired result when I use it on string fetched from database.

$result = DB::connection(\"connection\")->select(\"my          


        
相关标签:
1条回答
  • 2021-01-23 09:40

    It turns out you have Unicode dashes in your strings. To match all Unicode dashes, use

    /[\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.

    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.

    0 讨论(0)
提交回复
热议问题