PHP中截取中文乱码解决办法
如果ASCii码大于0xa0就是半个汉字;了解substr()、mb_substr()、ord()。 现在常用的编码格式是UTF8,GBK,GBK2312,BIG5。GBK是GBK2312的升级。现在我开发中遇到的基本都是UTF8,GBK常用在论坛BBS上。今天自己写了下UTF8的,测试下还说的过去,毕竟从网上拷下来的是可以用,但是如果有时间最好还是自己写一下,这样只有到自己手里的才是自己的。
最简的就是利用:例一
<?php echo substr("Hello world!", 6); //输出:world! ?>
例二:
<?php echo substr("Hello world!", 6, 5); ?>
英文的话没有问题,但是中文就出问题了,下面我们使用mb_substr函数处理。
<?php /** *—————————————————————————————————————————————————————————————————————————— *PHP有一个mbstring扩展库可以用(有朋友面试PHP的时候还被问到过这个问题哦),但是 * 一般的服务器都没打开php_mbstring.dll,需要在 *php.ini里把php_mbstring.dll打开,如果没有权限的话就只有联系你的ISP了。 *因为mb_string的效率高一些,所以我们先检查一下mb_string是否可以用: *—————————————————————————————————————————————————————————————————————————— */ ?> <?php if (function_exists('mb_string')) { mb_substr($string, $start, $length, $encoding); //other codes here } else mysubstr($string, $start, $length); // 调用自己的函数 ?>
首先定义自己的函数:
<?php /** *——————————————————————- *首先定义自己的函数: *——————————————————————- */ function mysubstr($string, $start, $length) { if (strlen($string) > $length) { $str = ""; $len = $start + $length; for ($i = $start; $i < $len; $i++) { if (ord(substr($string, $i, 1)) > 0xa0) { $str.= substr($string, $i, 2); $i++; } else { $str.= substr($string, $i, 1); } } return $str . '…'; } else { return $string; } } ?>
还有一个问题要注意,使用这种方法的时候,你的标签对里的charset只有设成中文的编码才可以,如GBK,GB2312。
下面自己常用的一个中英文字符截取函数,自定义截取中文字符串函数,基本上就是仿照mb_substr写个函数,直接调用罢了,以下为我找到的代码,其实很简单:
<?php function substr_cn($string_input, $start, $length) { /* 功能: * 此算法用于截取中文字符串 * 函数以单个完整字符为单位进行截取,即一个英文字符和一个中文字符均表示一个单位长度 * 参数: * 参数$string为要截取的字符串, * 参数$start为欲截取的起始位置, * 参数$length为要截取的字符个数(一个汉字或英文字符都算一个) * 返回值: * 返回截取结果字符串 * */ $str_input = $string_input; $len = $length; $return_str = ""; //定义空字符串 for ($i = 0; $i < 2 * $len + 2; $i++) $return_str = $return_str . " "; $start_index = 0; //计算起始字节偏移量 for ($i = 0; $i < $start; $i++) { if (ord($str_input{$start_index} >= 161)) //是汉语 { $start_index+= 2; } else //是英文 { $start_index+= 1; } } $chr_index = $start_index; //截取 for ($i = 0; $i < $len; $i++) { $asc = ord($str_input{$chr_index}); if ($asc >= 161) { $return_str{$i} = chr($asc); $return_str{$i + 1} = chr(ord($str_input{$chr_index + 1})); $len+= 1; //结束条件加1 $i++; //位置偏移量加1 $chr_index+= 2; continue; } else { $return_str{$i} = chr($asc); $chr_index+= 1; } } return trim($return_str); } //end of substr_cn ?>
本文地址:
转载随意,但请附上文章地址:-)

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



Call of Duty Warzone is a newly launched mobile game. Many players are very curious about how to set the language of this game to Chinese. In fact, it is very simple. Players only need to download the Chinese language pack, and then You can modify it after using it. The detailed content can be learned in this Chinese setting method introduction. Let us take a look together. How to set the Chinese language for the mobile game Call of Duty: Warzone 1. First enter the game and click the settings icon in the upper right corner of the interface. 2. In the menu bar that appears, find the [Download] option and click it. 3. Select [SIMPLIFIEDCHINESE] (Simplified Chinese) on this page to download the Simplified Chinese installation package. 4. Return to the settings

Excel spreadsheet is one of the office software that many people are using now. Some users, because their computer is Win11 system, so the English interface is displayed. They want to switch to the Chinese interface, but they don’t know how to operate it. To solve this problem, this issue The editor is here to answer the questions for all users. Let’s take a look at the content shared in today’s software tutorial. Tutorial for switching Excel to Chinese: 1. Enter the software and click the "File" option on the left side of the toolbar at the top of the page. 2. Select "options" from the options given below. 3. After entering the new interface, click the “language” option on the left

How to display Chinese characters correctly in PHPDompdf When using PHPDompdf to generate PDF files, it is a common challenge to encounter the problem of garbled Chinese characters. This is because the font library used by Dompdf by default does not contain Chinese character sets. In order to display Chinese characters correctly, we need to manually set the font of Dompdf and make sure to select a font that supports Chinese characters. Here are some specific steps and code examples to solve this problem: Step 1: Download the Chinese font file First, we need

VSCode Setup in Chinese: A Complete Guide In software development, Visual Studio Code (VSCode for short) is a commonly used integrated development environment. For developers who use Chinese, setting VSCode to the Chinese interface can improve work efficiency. This article will provide you with a complete guide, detailing how to set VSCode to a Chinese interface and providing specific code examples. Step 1: Download and install the language pack. After opening VSCode, click on the left

Title: An effective way to repair Chinese garbled characters in PHPDompdf. When using PHPDompdf to generate PDF documents, garbled Chinese characters are a common problem. This problem usually stems from the fact that Dompdf does not support Chinese character sets by default, resulting in Chinese content not being displayed correctly. In order to solve this problem, we need to take some effective ways to fix the Chinese garbled problem of PHPDompdf. 1. Use custom font files. An effective way to solve the problem of Chinese garbled characters in Dompdf is to use

"WWE2K24" is a racing sports game created by Visual Concepts and was officially released on March 9, 2024. This game has been highly praised, and many players are eagerly interested in whether it will have a Chinese version. Unfortunately, so far, "WWE2K24" has not yet launched a Chinese language version. Will wwe2k24 be in Chinese? Answer: Chinese is not currently supported. The standard version of WWE2K24 in the Steam Chinese region is priced at 199 yuan, the deluxe version is 329 yuan, and the commemorative edition is 395 yuan. The game has relatively high configuration requirements, and there are certain standards in terms of processor, graphics card, or running memory. Official recommended configuration and minimum configuration introduction:

Tips for solving Chinese garbled characters written by PHP into txt files. With the rapid development of the Internet, PHP, as a widely used programming language, is used by more and more developers. In PHP development, it is often necessary to read and write text files, including txt files that write Chinese content. However, due to encoding format problems, sometimes the written Chinese will appear garbled. This article will introduce some techniques to solve the problem of Chinese garbled characters written into txt files by PHP, and provide specific code examples. Problem analysis in PHP, text

How to change Chinese to English in Google Chrome? Some friends want to set Google Chrome to English so that they can continuously improve their English during use. So how to set it to English? Google Chrome is Chinese by default. Below, I will show you how to set the language of Google Chrome to English. Let’s take a look. Setting steps: 1. Open [Google Chrome], as shown in the figure below. 2. Click the [three dots] menu in the upper right corner of the Google Chrome interface, as shown in the figure below. 3. After entering the menu page, find [Settings], as shown in the figure below. 4. After entering the settings page, click the [Language] option, as shown in the figure below. 5. Select [Add Language] in the language interface, as shown in the figure below.
