Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
315
Q:

Php - regular expression to check if the string has chinese chars

Hey everyone,

I've been working on a web application recently, and I came across an interesting issue. I need to check if a given string contains any Chinese characters. I'm using PHP as the programming language for my application.

I know how to use regular expressions in PHP, but I'm not quite sure how to construct one that specifically checks for Chinese characters. I've tried searching online, but haven't found a clear solution yet.

Could someone please help me with this? I'm looking for a solution that allows me to verify if a string has any Chinese characters, ideally using regular expressions.

Any guidance or sample code would be greatly appreciated! Thank you in advance for your help.

Best regards,
[Your Name]

All Replies

mike.christiansen

Hey [Your Name],

I've actually encountered a similar requirement in one of my projects before. To check if a string contains Chinese characters using regular expressions in PHP, you can try the following approach.

php
function containsChineseChars($string) {
$pattern = '/\p{Han}/u';
return preg_match($pattern, $string);
}

// Example usage
$string1 = "Hello, 世界!";
$string2 = "This is a test.";

echo containsChineseChars($string1); // Output: 1 (contains Chinese characters)
echo containsChineseChars($string2); // Output: 0 (no Chinese characters)


In this code snippet, we define a function `containsChineseChars()` that takes a string as input. The regular expression pattern `/\p{Han}/u` matches any Chinese character. The `/u` modifier is used to ensure proper handling of Unicode characters.

The `preg_match()` function checks if the pattern is present in the input string. If there are Chinese characters, it returns `1`, otherwise `0`.

Feel free to give this approach a try, and let me know if you have any further questions. Good luck with your project!

Best regards,
User 1

berge.axel

Hey there,

I've actually faced a similar situation in one of my PHP projects where I needed to check if a string contained Chinese characters. While regular expressions can be handy, I found an alternative way that worked well for me.

Instead of using regular expressions, I utilized the `mb_detect_encoding()` function in PHP, which detects the character encoding of a string. Here's an example of how you can use it to check for Chinese characters:

php
function containsChineseChars($string) {
$encoding = mb_detect_encoding($string, "UTF-8, GB2312, GBK, BIG5");

if (strpos($encoding, 'GB') !== false || strpos($encoding, 'BIG') !== false) {
return true;
}

return false;
}

// Example usage
$string1 = "Hello, 世界!";
$string2 = "This is a test.";

echo containsChineseChars($string1); // Output: 1 (contains Chinese characters)
echo containsChineseChars($string2); // Output: 0 (no Chinese characters)


In this approach, we use the `mb_detect_encoding()` function with a list of possible Chinese character encodings: UTF-8, GB2312, GBK, and BIG5. We then check if the detected encoding contains "GB" or "BIG", which are commonly associated with Chinese characters.

Give this approach a try and see if it fits your requirements. Let me know if you have any further questions or need clarification!

Best regards,
User 2

New to LearnPHP.org Community?

Join the community