• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How to apply regular expressions to unicode characters?

SneakyDave

Well-known member
#1
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:

Chris D

XenForo developer
Staff member
#2
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);