How to apply regular expressions to unicode characters?


Well-known member
I have a regular expression that replaces characters and numbers in a string. I use preg_replace and a regular expression of "/[a-z\d]/i" to do that.
$newtext= preg_replace("/[a-z\d]/i", "x", $originaltext);

Is there an easy way to extend that regex to include unicode characters?

Edited to add, after a bit of searching, could I reaplce [a-z] with this to include "any kind of letter from any language"?
$newtext= preg_replace("/[\p{L}\d]/i", "x", $originaltext);
Last edited:

Chris D

XenForo developer
Staff member
I think you can just use the /u modifier and that will include unicode characters amongst the normal [a-z] range.

$newtext = preg_replace("/[a-z\d]/iu", "x", $originaltext);