


Solution to garbled Chinese characters intercepted by php function substr
This article introduces the solution to the problem of garbled characters when intercepting Chinese characters using PHP's string interception function substr. Friends in need can refer to it.
php string interception function substr: string substr ( string $string , int $start [, int $length ] ) Returns the string with length length starting from the start position in string The substr function intercepts characters by bytes. Chinese characters are 2 bytes when encoded in GB2312, and 3 bytes when encoded by utf-8. Therefore, when intercepting a string of specified length, if the Chinese characters are truncated , then the returned results will be garbled when displayed. Two solutions are provided below for your reference. 1, use mb_substr function instead string mb_substr ( string $str , int $start [, int $length [, string $encoding ]] ) Similar to the substr() function, but counting is based on the number of characters to ensure character safety Use the mb_substr() function to ensure that there will be no garbled characters. Disadvantages: Length statistics become character count statistics, not by byte count statistics. When used for display, there will be a large difference in display length between Chinese results and English results of the same length. 2. Self-built function enhances substr function Chinese characters are calculated in 2 length units, so that the final display length of the string interception result in a mixed Chinese and English environment is close; The last incomplete character is discarded to ensure that there will be no garbled characters on display; it is also compatible with UTF-8 encoding and GB2312 encoding commonly used for Chinese characters, and has good versatility. The complete code is as follows (strtolower function is used): <?php /** * 增强型字符串截取函数 * 截取中文字符无乱码 * edit bbs.it-home.org */ function getstr($string, $length, $encoding = 'utf-8') { $string = trim($string); if($length && strlen($string) > $length) { //截断字符 $wordscut = ''; if(strtolower($encoding) == 'utf-8') { //utf8编码 $n = 0; $tn = 0; $noc = 0; while ($n < strlen($string)) { $t = ord($string[$n]); if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) { $tn = 1; $n++; $noc++; } elseif(194 <= $t && $t <= 223) { $tn = 2; $n += 2; $noc += 2; } elseif(224 <= $t && $t < 239) { $tn = 3; $n += 3; $noc += 2; } elseif(240 <= $t && $t <= 247) { $tn = 4; $n += 4; $noc += 2; } elseif(248 <= $t && $t <= 251) { $tn = 5; $n += 5; $noc += 2; } elseif($t == 252 || $t == 253) { $tn = 6; $n += 6; $noc += 2; } else { $n++; } if ($noc >= $length) { break; } } if ($noc > $length) { $n -= $tn; } $wordscut = substr($string, 0, $n); } else { for($i = 0; $i < $length - 1; $i++) { if(ord($string[$i]) > 127) { $wordscut .= $string[$i].$string[$i + 1]; $i++; } else { $wordscut .= $string[$i]; } } } $string = $wordscut; } return trim($string); } // 示例 echo getstr("0一二三四五六七",1).'<br />'; // 0 echo getstr("0一二三四五六七",2).'<br />'; // 0 echo getstr("0一二三四五六七",3).'<br />'; // 0一 echo getstr("0一二三四五六七",4).'<br />'; // 0一 echo getstr("0一二三四五六七",5).'<br />'; // 0一二 echo getstr("0一a二b三四五六七",1).'<br />'; // 0 echo getstr("0一a二b三四五六七",2).'<br />'; // 0 echo getstr("0一a二b三四五六七",3).'<br />'; // 0一 echo getstr("0一a二b三四五六七",4).'<br />'; // 0一a echo getstr("0一a二b三四五六七",5).'<br />'; // 0一a //此函数由UCHome 1.5中的getstr()函数修改而来。 ?> Copy after login |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov
