php - Regex to replace punctuation -
i've been trying few hours work effect need nothing works quite should. i'm building discussion board type thing , have made way tag other users putting @username in post text.
currently have code strip wouldn't part of username once tags have been pulled out of entire text:
$name= preg_replace("/[^a-za-z0-9_]/",'',$name); this works because correct captures names example (@username), @username:, @username, text etc. (so remove ,, :, , )).
however, not work when user has non-ascii characters in username. example if it's @üsername, result of line above gives sername not useful.
is there way using preg_replace still strip these additional punctuation, retain non-ascii letters?
any appreciated :)
to detect punctuation characters, can use unicode property \p{p} instead:
$name = preg_replace('/[\p{p} ]+/', '', $name);
Comments
Post a Comment