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); 

regex demo


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

qt - How to embed QML toolbar and menubar into QMainWindow -