Several ways to split Chinese and English strings in PHP
This article introduces the method of splitting Chinese and English strings in PHP, calculating the total length of characters, intercepting the string from the left side, and cutting the string into an array according to the given text. Friends in need can refer to it.
Split a piece of text according to the number of words, because the text may be a mixture of Chinese and English, and the PHP function strlen can only calculate the number of bytes of the string, so I implemented several functions and shared them. Example 1, calculate the total length of characters. <?php function ccStrLen($str) #计算中英文混合字符串的长度 { $ccLen=0; $ascLen=strlen($str); $ind=0; $hasCC=ereg(”[xA1-xFE]“,$str); #判断是否有汉字 $hasAsc=ereg(”[x01-xA0]“,$str); #判断是否有ASCII字符 if($hasCC && !$hasAsc) #只有汉字的情况 return strlen($str)/2; if(!$hasCC && $hasAsc) #只有Ascii字符的情况 return strlen($str); for($ind=0;$ind<$ascLen;$ind++) { if(ord(substr($str,$ind,1))>0xa0) { $ccLen++; $ind++; } else { $ccLen++; } } return $ccLen; } ?> Copy after login Example 2, intercept the string from the left side. <?php function ccStrLeft($str,$len) #从左边截取中英文混合字符串 { $ascLen=strlen($str); if($ascLen<=$len) return $str; $hasCC=ereg(”[xA1-xFE]“,$str); #同上 $hasAsc=ereg(”[x01-xA0]“,$str); if(!$hasCC) return substr($str,0,$len); if(!$hasAsc) if($len & 0×01) #如果长度是奇数 return substr($str,0,$len+$len-2); else return substr($str,0,$len+$len); $cind=0;$flag=0;$reallen=0;//实际取字节长 while($cind<$ascLen && $reallen<$len) { //by bbs.it-home.org if(ord(substr($str,$cind,1))<0xA1){ //如果该字节为英文 则加一 $cind++; }else{//否则 加2个字节 $cind+=2; } $reallen++; } return substr($str,0,$cind); } ?> Copy after login Example 3, store the given text into an array according to the number of cuts (suitable for short text, long articles can be processed directly without dividing a part) <?php function SplitContent($content,$smslen){ $str_tmp=$content; $arr_cont=array(); $len_tmp=0; $i=0;//分割绝对位置 while (strlen($str_tmp)>0){ $str_tmp=ccStrLeft($str_tmp,$smslen); array_push($arr_cont,$str_tmp); $i+=strlen($str_tmp); $str_tmp=substr($content,$i,strlen($content)); } return $arr_cont; } //by bbs.it-home.org ?> Copy after login Test: <?php $str=’a计算中英文混合1234字符串的长度abcd’; echo $str.’的长度为:’.ccStrLen($str); echo ‘<br>’; $smslen=3;//截取长度 print_r(SplitContent($str,$smslen)); ?> Copy after login Segmentation results: Array ( [0] => a calculation [1] => Chinese and English [2] => Mix 1 [3] => 234 [4] => string [5] => length of [6] => abc [7] => d ) |

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
