How to apply regular expressions to unicode characters?

SneakyDave

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.
Code:
$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"?
Code:
$newtext= preg_replace("/[\p{L}\d]/i", "x", $originaltext);
 
Last edited:
I think you can just use the /u modifier and that will include unicode characters amongst the normal [a-z] range.

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