开发者

php multi byte strings regex

开发者 https://www.devze.com 2023-03-27 15:06 出处:网络
We have a regex to strip out non alpha numeric characters except for \'#\', \'&\' and \'-\'. Here is what it looks like:

We have a regex to strip out non alpha numeric characters except for '#', '&' and '-'. Here is what it looks like:

preg_replace('/[^a-zA-Z0-9#&-*]/', '', strtolower($title开发者_如何学C));

Now we need to support traditional Chinese strings and the above function won't work. How can I implement similar functionality for traditional Chinese.

Thanks,


Use u modifier:

preg_replace(`/[^a-zA-Z0-9#&-*诶]/u`, '', $string);

By the way, don't use strtolower(), because it will break your string. Use mb_strtolower():

mb_strtolower($string, 'UTF-8');


Have you tried mb_ereg_replace() instead of preg_replace()? That might do the trick.

http://www.php.net/manual/en/function.mb-ereg-replace.php

0

精彩评论

暂无评论...
验证码 换一张
取 消