Method of Intercepting Chinese English Mixed Strings by php
- 2021-10-15 10:10:03
- OfStack
php interception should be a string, so there is no need to use substr or mb_substr
// Intercept a string like this
a Li 3
Using ASCII
/**
*
* Chinese-English mixed string interception
* @param unknown_type $sourcestr
* @param unknown_type $cutlength
*/
function assoc_substr($sourcestr, $cutlength) {
$returnstr = '';
$i = 0;
$n = 0;
$str_length = strlen ( $sourcestr ); // Number of bytes of string
while ( ($n < $cutlength) and ($i <= $str_length) ) {
$temp_str = substr ( $sourcestr, $i, 1 );
$ascnum = Ord ( $temp_str ); // Get the first in the string $i Bit character ascii Code
if ($ascnum >= 224) {// If ASCII Position height and 224 ,
$returnstr = $returnstr . substr ( $sourcestr, $i, 3 ); // According to UTF-8 Coding specification, which will 3 Consecutive characters are counted as a single character
$i = $i + 3; // Actual Byte Count as 3
$n ++; // String length meter 1
} elseif ($ascnum >= 192){ // If ASCII Position height and 192 ,
$returnstr = $returnstr . substr ( $sourcestr, $i, 2 ); // According to UTF-8 Coding specification, which will 2 Consecutive characters are counted as a single character
$i = $i + 2; // Actual Byte Count as 2
$n ++; // String length meter 1
} elseif ($ascnum >= 65 && $ascnum <= 90) {// If it's a capital letter,
$returnstr = $returnstr . substr ( $sourcestr, $i, 1 );
$i = $i + 1; // Actual Byte Counting 1 A
$n ++; // However, considering the overall beauty, the capital letters are calculated 1 High-order characters
}elseif ($ascnum >= 97 && $ascnum <= 122) {
$returnstr = $returnstr . substr ( $sourcestr, $i, 1 );
$i = $i + 1; // Actual Byte Counting 1 A
$n ++; // However, considering the overall beauty, the capital letters are calculated 1 High-order characters
} else {// In other cases, half-angle punctuation marks,
$returnstr = $returnstr . substr ( $sourcestr, $i, 1 );
$i = $i + 1;
$n = $n + 0.5;
}
}
return $returnstr;
}
Summarize