Are preg_match and preg_replace Multi-Byte Safe?

The direct answer is yes. Both preg_match and preg_replace functions can be multi-byte safe. But at least, you have to meet the following two requirements to make them work well.

1. Use u as one of your modifiers to the pattern.

For example, I would like to match a word "中" in a string, I should do this:

$str = "数据库中介";
$flag = preg_match("/中/u", $str);
...

In the above regular expression pattern, the little u is the modifier for notifying PHP engine to match the pattern in UTF-8 fashion.

For more information about modifiers, you may refer to the official documentation: PHP: Possible modifiers in regex patterns - Manual

2. Save your PHP scripts as UTF-8 encoded files.

If your files are encoded in MS950, I'm sorry, they can't work well. You have to change the encoding into UTF-8 then save.

You may refer to this following post for further guidance about change text encoding. How to Change File Encoding in Eclipse.

Leave a Reply

Your email address will not be published. Required fields are marked *