实用技巧:PHP截取中文字符串的问题
技巧|问题|中文|字符串
以下代码试用于GB2312编码,截取中文字符串是PHP中一个头疼的问题,解决方法是根据值是否大于等于128来判断是否是双字节字符,以避免出现乱码的情况。但中英文混合、特殊符号等问题总是存在,现在写一个比较全面的,仅供参考:
程序说明:
1. len 参数以中文字符为标准,1len等于2个英文字符,为了形式上好看些
2. 如果将magic参数设为false,则中文和英文同等看待,取绝对的字符数
3. 特别适用于用htmlspecialchars()进行过编码的字符串
4. 能正确处理GB2312中实体字符模式()
程序代码:
<ccid_code>function FSubstr($title,$start,$len="",$magic=true) {/** * powered by Smartpig * mailto:d.einstein@263.net */ $length = 0;if($len == "") $len = strlen($title);//判断起始为不正确位置if($start > 0){ $cnum = 0; for($i=0;$i= 128) $cnum ++; } if($cnum%2 != 0) $start--; unset($cnum);}if(strlen($title)?) if(substr($title,$i,4)="=" ?) ?&?) if($cur="=" $cur="substr($title,$i,1);">=128) { $cstep = 2; $length += 2; $i += 1; $realnum ++; if($magic) { $blen ++; $ctype = 1; } }else{ $cstep = 1; $length +=1; $realnum ++; if($magic) { $alen++; } } } if($magic) { if(($blen*2+$alen) == ($len*2)) break; if(($blen*2+$alen) == ($len*2+1)) { if($ctype == 1) { $length -= $cstep; break; }else{ break; } } }else{ if($realnum == $len) break; }}unset($cur);unset($alen);unset($blen);unset($realnum);unset($ctype);unset($cstep);return substr($title,$start,$length);}</ccid_code>

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



Solution to docker start failure: 1. Check the running status, and then release the occupied memory through the "echo 3 > /proc/sys/vm/drop_caches" command; 2. Use "$netstat -nltp|grep .. ." command to check whether the port has been occupied. If it is found to be occupied after going online, change it to an available port and restart.

Title is the meaning that defines the title of the web page. It is located within the tag and is the text displayed in the title bar of the browser. Title is very important for the search engine optimization and user experience of the web page. When writing HTML web pages, you should pay attention to using relevant keywords and attractive descriptions to define the title element to attract more users to click and browse.

Solution to node start error: 1. Execute "node xx.js" directly in the terminal; 2. Add start startup item "scripts": {"test": "echo \"Error: no test specified\" && exit 1 ","start":"node service.js"}"; 3. Re-execute "npm start".

The title in HTML displays the title tag of the web page, which allows the viewer to know what the current page is about, so each web page should have a separate title.

publicclassTest1extendsThread{@Overridepublicvoidrun(){while(true){System.out.println(Thread.currentThread().getName());}} publicstaticvoidmain(String[]args){Test1test1=newTest1();test1.run() ;//Output result maintest1.start();//Output result Thread-0}}1.startstart is to start a

start method and run method The $start()$ method is used to start a thread. At this time, the thread is in the ready (runnable) state and is not running. Once the $cpu$ time slice is obtained, the $run()$ method starts to be executed. . Directly calling the $run()$ method only calls a method in a class, which is essentially executed in the current thread. Therefore, it can only be achieved by using the $start()$ method to call the $run()$ method. True multithreading. Sample code@Slf4j(topic="c.Test4")publicclassTest4{publicstaticvoidmain(Strin

PHP is a popular server-side programming language that provides many useful features, one of which is manipulating arrays. When using PHP arrays, sometimes you need to delete elements. In this case, you can use the unset() function. The unset() function is a function used in PHP to delete variables and can be used to delete elements in an array. Its syntax is as follows: unset(array$array[,mixed$key1[,mixed$key2[,mixed

The session_start() function is one of the functions that opens a session in PHP. Session is a very common technology in web development, and the basis of session is to assign a unique session ID to the user when he visits the website, and to use this session ID to identify the user in subsequent visits. Sessions can be used to save the user's login status, shopping cart contents, and other user information, and can also be used to implement permission control for some functions on the website. The way to open a session in PHP is very simple, just use sess
