PHP初记|中英混合字符串截取
PHP小记|中英混合字符串截取
常常在WEB页面显示记录列表的时候需要对过长内容进行截取。
采用PHP内置的substr函数对中英混合字符串,特别是字符编码在UTF-8的情况下,支持非常不好,会出现乱码。
所以自己写了一个函数:
?
function truncate($string, $len, $wordsafe = FALSE) { $slen = strlen($string); if ($slen <= $len) { return $string; } if ($wordsafe) { while (($string[-- $len] != ' ') && ($len > 0)) { }; } if ((ord($string[$len]) < 0x80) || (ord($string[$len]) >= 0xC0)) { return substr($string, 0, $len) . "..."; } while (ord($string[-- $len]) < 0xC0) { }; return substr($string, 0, $len) . "..."; }
?
经测试成功。YEAH!
?
=======================================================================
2012-06-15更新:
?
今天再次写了一个,好处是会把两个英文字符当作一个汉字字符的长度:
所以传递的需要截取多少个汉字的长度
?
?
function truncate($string, $len, $cnCharWidth = 2) { $len = $len * $cnCharWidth; $suffix = "..."; $newStr = ""; for ($i = 0, $j = 0; $i < $len; $i++, $j++) { if (!isset($string[$j])) { $suffix = ""; break; } $start = $j; while ($j < ($start +3) && !(ord($string[$j]) < 0x80)) { $j++; } if ($start == $j) { $charLen = 1; } else { $i = $i + 1; $j--; $charLen = 3; } $newStr .= substr($string, $start, $charLen); } return $newStr . $suffix; }

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

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

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.

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

Hello everyone, today I will share with you the basic knowledge of Java: String. Needless to say the importance of the String class, it can be said to be the most used class in our back-end development, so it is necessary to talk about it.

In Golang programming, byte, rune and string types are very basic and common data types. They play an important role in processing data operations such as strings and file streams. When performing these data operations, we usually need to convert them to each other, which requires mastering some conversion skills. This article will introduce the byte, rune and string type conversion techniques of Golang functions, aiming to help readers better understand these data types and be able to apply them skillfully in programming practice.
