


Collection of several PHP string interception functions that support Chinese_PHP tutorial
字符串截取是一个非常常见的编程任务,而往往带中文的字符串截取会经常用到。虽然不难,但是自己写函数实现又耗费时间,这里介绍一个比较好用的字符串截取函数,能够胜任基本的需求了。
<?php function sysSubStr($string,$length,$append = false) { if(strlen($string) <= $length ) { return $string; } else { $i = 0; while ($i < $length) { $stringTMP = substr($string,$i,1); if ( ord($stringTMP) >=224 ) { $stringTMP = substr($string,$i,3); $i = $i + 3; } elseif( ord($stringTMP) >=192 ) { $stringTMP = substr($string,$i,2); $i = $i + 2; } else { $i = $i + 1; } $stringLast[] = $stringTMP; } $stringLast = implode("",$stringLast); if($append) { $stringLast .= "..."; } return $stringLast; } } $string = "帮客之家 —— 专注于互联网主流的各种技术"; $length = "27"; $append = true; echo sysSubStr($string,$length,$append); // 输出 // 帮客之家 —— 专... ?>
截取GB2312中文字符串:
<?php //截取中文字符串 function mysubstr($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; } ?>
截取utf8编码的多字节字符串:
<?php //截取utf8字符串 function utf8Substr($str, $from, $len) { return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'. '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s', '$1',$str); } ?>
UTF-8、GB2312都支持的汉字截取函数:
<?php /* Utf-8、gb2312都支持的汉字截取函数 cut_str(字符串, 截取长度, 开始长度, 编码); 编码默认为 utf-8 开始长度默认为 0 */ function cut_str($string, $sublen, $start = 0, $code = 'UTF-8') { if($code == 'UTF-8') { $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"; preg_match_all($pa, $string, $t_string); if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."..."; return join('', array_slice($t_string[0], $start, $sublen)); } else { $start = $start*2; $sublen = $sublen*2; $strlen = strlen($string); $tmpstr = ''; for($i=0; $i< $strlen; $i++) { if($i>=$start && $i< ($start+$sublen)) { if(ord(substr($string, $i, 1))>129) { $tmpstr.= substr($string, $i, 2); } else { $tmpstr.= substr($string, $i, 1); } } if(ord(substr($string, $i, 1))>129) $i++; } if(strlen($tmpstr)< $strlen ) $tmpstr.= "..."; return $tmpstr; } } $str = "abcd需要截取的字符串"; echo cut_str($str, 8, 0, 'gb2312'); ?>

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

"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:

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

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

Some friends may accidentally set it to English when installing the system. As a result, all the interfaces are changed to English and they cannot be understood. In fact, we can set the language in the control panel and change the language to Chinese. Let’s take a look at how to change it. How to change the language in win7 to Chinese 1. First click the button in the lower left corner of the screen, and then select "Control Panel" 2. Find "Changedispalylanguage" under "Clock, Language, and Region" 3. Click "English" below to select from the drop-down menu Simplified Chinese. 4. After confirmation, click "Logoffnow" to log out and restart the computer. 5. After coming back

Quark browser software provides countless resource information, and the search results are the most accurate. It has a powerful built-in search engine. You can find the corresponding answers by entering keywords. The overall Internet experience is very good. Browsing sections in different fields are open. Search for whatever you search and see. You can find it immediately. All annoying advertising pop-ups are blocked. All annoying advertising pop-ups are blocked. You can freely switch between different browsing modes and surf the Internet. There is no lag at all in the process, and of course the translation function can be turned on. Languages of any country can be easily translated, and there are no problems in communication. Files and documents compatible with various formats can be opened and viewed. Now the editor is browsing for Quark online in detail. Server users bring the method of setting Chinese translation. 1. First click on the mobile phone desktop
