Home > Article > Backend Development > PHP implements the solution to intercept Chinese strings without the ? sign_php tips
This article mainly introduces the solution for PHP to intercept Chinese strings without the ? sign, involving PHP string traversal and encoding conversion and other related operating techniques. Friends in need can refer to it
The details are as follows :
When PHP intercepts a mixed string of Chinese and English, the last Chinese character is often split into two halves, for example: intercepting the first 18 characters of the string
<?php $text = "1欢迎访问sina新浪播客"; $value = substr($text, 0, 18); echo $value."<BR>"; ?>
The output is:
1欢迎访问新浪?BR>
So I wrote the following code to determine if the Chinese character bytes in a mixed Chinese and English string If the number is an odd number, one less byte will be intercepted to ensure complete display of Chinese characters.
<?php $text = "1欢迎访问sina新浪播客"; $value = substr($text, 0, 18); $value_length = strlen($value); $value_count = 0; for ($i = 0; $i < $value_length; $i++) { if (ord($value{$i}) > 127) { $value_count++; } } if ($value_count % 2 != 0) { $value = substr($text, 0, $value_length - 1); } echo $value."<BR>"; ?>
The output is:
1欢迎访问sina新浪
and above That’s the entire content of this article, I hope it will be helpful to everyone’s study.
Related recommendations:
Detailed explanation of how to intercept the length of string in PHP
PHPStringReverse order implementation method
php implementation to obtain a specified number of random string method
The above is the detailed content of PHP implements the solution to intercept Chinese strings without the ? sign_php tips. For more information, please follow other related articles on the PHP Chinese website!