Home php教程 php手册 php substr截断汉字乱码解决办法

php substr截断汉字乱码解决办法

May 25, 2016 pm 04:50 PM
substr Truncate

在php中substr是用来截取字符的,但是有朋友会发现把它来截英文字母是没有任何问题,但是如果截中文时会出现半个汉字乱码了,下面我来介绍一些解决办法.

substr() 函数返回字符串的一部分。

语法:substr(string,start,length)

实例代码如下:

<?php
echo substr("Hello world!", 6);
?>
Copy after login

输出 world!

实例代码如下:echo substr('中国文',1);

结果就是筹码了,后来才知道中文与英文的区别在于内编码了,一个网站这样说到substr函数在截取字符时是按字节来截取的,中文字符在GB2312编码时为2个字节,utf-8编码时为3个字节,所以截取指定长度的字符串时如果截断了汉字,那么返回的结果显示出来便会出现乱码.

解决办法:1、利用mb_substr来截取,代码如下:

<?php
$str = &#39;这样一来我的字符串就不会有乱码^_^&#39;;
echo "mb_substr:" . mb_substr($str, 0, 7, &#39;utf-8&#39;);
//结果:这样一来我的字
echo "<br>";
echo "mb_strcut:" . mb_strcut($str, 0, 6, &#39;utf-8&#39;);
//结果:这样

?>
Copy after login

但是如果要使用mb_substr截取我们需要使用使用mbstring扩展库,如果没有权限的朋友我们就可参考下面函数,代码如下:

<?php
function msubstr($str, $start, $len) {
    $tmpstr = "";
    $strlen = $start + $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;
}
?>
Copy after login

            

教程链接:

随意转载~但请保留教程地址★

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
How to use the TRUNCATE function in MySQL to truncate numbers to specify the number of decimal places for display How to use the TRUNCATE function in MySQL to truncate numbers to specify the number of decimal places for display Jul 26, 2023 pm 01:51 PM

How to use the TRUNCATE function in MySQL to truncate a number to specify the number of decimal places for display in the MySQL database. If we need to truncate a number to specify the number of decimal places for display, we can use the TRUNCATE function. The TRUNCATE function can help us achieve precise truncation of numbers, thereby retaining data with a specified number of decimal places. The syntax of the TRUNCATE function is as follows: TRUNCATE(number,decimal_places)where, numb

PHP returns the ASCII value of the first character of the string PHP returns the ASCII value of the first character of the string Mar 21, 2024 am 11:01 AM

This article will explain in detail the ASCII value of the first character of the string returned by PHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP returns the ASCII value of the first character of a string Introduction In PHP, getting the ASCII value of the first character of a string is a common operation that involves basic knowledge of string processing and character encoding. ASCII values ​​are used to represent the numeric value of characters in computer systems and are critical for character comparison, data transmission and storage. The process of getting the ASCII value of the first character of a string involves the following steps: Get String: Determine the string for which you want to get the ASCII value. It can be a variable or a string constant

PHP returns the string from the start position to the end position of a string in another string PHP returns the string from the start position to the end position of a string in another string Mar 21, 2024 am 10:31 AM

This article will explain in detail how PHP returns the string from the start position to the end position of a string in another string. The editor thinks it is quite practical, so I share it with you as a reference. I hope you will finish reading this article. You can gain something from this article. Use the substr() function in PHP to extract substrings from a string. The substr() function can extract characters within a specified range from a string. The syntax is as follows: substr(string,start,length) where: string: the original string from which the substring is to be extracted. start: The index of the starting position of the substring (starting from 0). length (optional): The length of the substring. If not specified, then

Cannot truncate a table referenced in a foreign key constraint - How to solve MySQL error: Cannot truncate a table referenced in a foreign key constraint Cannot truncate a table referenced in a foreign key constraint - How to solve MySQL error: Cannot truncate a table referenced in a foreign key constraint Oct 05, 2023 pm 03:29 PM

Title: Unable to truncate tables referenced by foreign key constraints - How to solve MySQL error Summary: When using the MySQL database management system, we often encounter the problem of being unable to truncate tables referenced by foreign key constraints. This article will detail the cause of this error and provide solutions, including specific code examples, to help readers better understand and solve this problem. Text: Introduction In database design, foreign keys are one of the important mechanisms used to establish associations between different tables. Foreign key constraints can ensure data integrity and consistency. However, when I

Use the PHP function 'substr' to get the substring of a string Use the PHP function 'substr' to get the substring of a string Jul 24, 2023 pm 10:13 PM

Use the PHP function "substr" to obtain the substring of a string. In PHP programming, you often encounter situations where you need to obtain part of the content of a string. At this time, we can use PHP's built-in function "substr" to achieve this. This article explains how to use the "substr" function to get a substring of a string and provides some code examples. 1. Basic usage of the substr function The substr function is used to obtain a substring of a specified length from a string. Its basic syntax is as follows: substr(

Understand the substr() function in PHP to intercept strings Understand the substr() function in PHP to intercept strings Nov 18, 2023 am 11:27 AM

Understand the substr() function in PHP for intercepting strings. In the PHP language, the substr() function is a very useful string processing function, which can be used to intercept string fragments at a specified position and length. The substr() function accepts three parameters: the string to be intercepted, the starting position of the interception, and the length of the interception. Below we will introduce the use of the substr() function in detail and give specific code examples. Basic usage of substr() function substr() function

PHP mb_substr function invalid solution PHP mb_substr function invalid solution Mar 22, 2024 am 09:00 AM

Solutions for invalid PHPmb_substr function When developing PHP applications, the mb_substr function is often used to intercept strings. However, sometimes you may encounter situations where the mb_substr function is invalid, mainly due to character encoding issues in different environments. In order to solve this problem, we need to effectively handle the mb_substr function. A common solution is to ensure that the mb_substr function can

PHP convert first letter of string to lowercase PHP convert first letter of string to lowercase Mar 21, 2024 pm 02:11 PM

This article will explain in detail how PHP converts the first letter of a string to lowercase. I think it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. Converting the first letter of a PHP string to lowercase Introduction In PHP, converting the first letter of a string to lowercase is a common operation. This can be achieved by using the built-in function lcfirst() or the string operator strtolower(). This guide will dive into both approaches, providing example code and best practices. Method 1: Use the lcfirst() function The lcfirst() function is specifically designed to convert the first letter of a string to lowercase while leaving the rest of the characters unchanged. Its syntax is as follows: st

See all articles