


PHP solves the problem of garbled Chinese characters intercepted by substr()
In php, if I want to use substr() to intercept a string that is all in English, it will be no problem. If it includes Chinese or English, it will be a tragedy, but don’t worry, we can use other methods to solve it.
When php intercepts Chinese strings, garbled characters appear. This is a recent discovery. I have previously written an article about automatically generating meta information. That article was about using php to intercept the first few words of the article as description method, but IE6 cannot load CSS. Here is a supplement.
First of all, we need to clarify this problem. The reason why IE6 occasionally fails to load CSS is because the file is garbled, causing the subsequent link to load CSS to not be correctly parsed by IE6. So I saw a pure HTML page, no CSS, naked! After clarifying the problem, the remaining problem can be easily solved, which is to prevent garbled characters. Since the function provided by Wange has garbled characters, I found a new PHP function to solve this garbled code problem.
The substr() function can split text, but problems often occur if the text to be split includes Chinese characters.
mb_substr() The usage of this function is similar to substr(), except that one more parameter is added at the end to set the encoding of the string.
After reading this, you should understand the reason why I improved the Wange method~~
Here are some more advanced processing methods
Example 1
The code is as follows
function func_chgtitle($str,$len) { //$length我们允许字符串显示的最大长度 $tmpstr = ""; $strlen = $len; for($i = 0; $i < $strlen; $i++) { if(ord(substr($str, $i, 1)) > 0xa0) { $tmpstr .= substr($str, $i, 2); $i++; } else $tmpstr .= substr($str, $i, 1); } return $tmpstr; }
Example 2
The string is encoded as UTF-8, and one Chinese character occupies three bytes:
public static function chinesesubstr($str, $start, $len) { // $str refers to the string, $start refers to the starting position of the string, $len refers to the length of the string
$strlen = $start + $len; // Use $strlen to store the total length of the string, that is, from the starting position of the string to the total length of the string
The code is as follows
for($i = $start; $i < $strlen;) { if (ord ( substr ( $str, $i, 1 ) ) > 0xa0) { // 如果字符串中首个字节的ASCII序数 值大于0xa0,则表示汉字 $tmpstr .= substr ( $str, $i, 3 ); // 每次取出三位字符赋给变量$tmpstr,即等 于一个汉字 $i=$i+3; // 变量自加3 } else{ $tmpstr .= substr ( $str, $i, 1 ); // 如果不是汉字,则每次取出一位字符赋给 变量$tmpstr $i++; } } return $tmpstr; // 返回字符串 }
I hope this article will help everyone deal with the same problem with PHP programming!
For more php related articles on solving the problem of garbled Chinese characters intercepted by substr(), please pay attention to the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

Alipay PHP...

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
