preg_replace
does not return desired result when I use it on string fetched from database.
$result = DB::connection(\"connection\")->select(\"my
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.