


The article introduces in detail the differences and usage of strlen, mb_strlen, substr(), mb_substr() and mb_strcut. Students who need to learn can refer to it.
About the use of the string splitting function of mb_*:
Configuration under win
Need to install php_mbstring.dll extension
You need to open php_mbstring.dll in php.ini
The configuration under Linux can be easily searched online
The code is as follows | Copy code | ||||
$str='Chinese character a'; echo strlen($str).' ';//14
echo mb_strlen($str,'utf8').' ';//8
echo mb_strlen($str,'gb2312').'
|
Result analysis: When calculating strlen, a UTF8 Chinese character is treated as 3 lengths, so the length of "Chinese a character 1 character" is 3*4+2=14. When calculating mb_strlen, the internal code is selected as UTF8, then A Chinese character will be calculated as a length of 1, so the length of "Chinese a character 1 character" is 6
mb_strlen default encoding can be passed
Obtained by mb_internal_encoding().
1 echo (strlen($str) + mb_strlen($str,'UTF8')) / 2;
PHP’s built-in string length function strlen cannot correctly handle Chinese strings. It only gets the number of bytes occupied by the string. For GB2312 Chinese encoding, the value obtained by strlen is twice the number of Chinese characters, while for UTF-8 encoded Chinese, the difference is three times (under UTF-8 encoding, one Chinese character occupies 3 bytes).
String splitting
The substr() function can split text, but if the text to be split includes Chinese characters, you will often encounter problems. In this case, you can use the mb_substr()/mb_strcut function代码如下 | 复制代码 |
echo mb_substr('这样一来我的字符串就不会有乱码^_^', 0, 7, 'utf-8'); ?> |
mb_substr splits characters by words, while mb_strcut splits characters by bytes, but neither will produce half a character.
代码如下 | 复制代码 |
echo mb_strcut('这样一来我的字符串就不会有乱码^_^', 0, 7, 'utf-8'); ?> |
The code is as follows | Copy code |
echo mb_substr('This way my string will not be garbled^_^', 0, 7, 'utf-8'); ?> |
The code is as follows | Copy code |
echo mb_strcut('This way my string will not be garbled^_^', 0, 7, 'utf-8'); ?> |
Output: like this
As can be seen from the above example, mb_substr splits characters by words, while mb_strcut splits characters by bytes, but neither of them will produce half a character...
Description of mbstring function:
PHP's mbstring extension module provides multi-byte character processing capabilities. The most commonly used method is to use mbstring to split multi-byte Chinese characters. This can avoid the occurrence of half characters. Since it is an extension of PHP, its The performance is also better than some custom multi-byte segmentation functions.
The mbstring extension provides several functions with similar functions, mb_substr and mb_strcut. See their explanation in the manual.
mb_substr
mb_substr() returns the portion of str specified by the start and length parameters.
mb_substr() performs multi-byte safe substr() operation based on number of characters. Position is sqlserver/42852.htm target=_blank >counted from the beginning of str. First character's position is 0. Second character position is 1 , and so on.
mb_strcut
mb_strcut() returns the portion of str specified by the start and length parameters.
mb_strcut() performs equivalent operation as mb_substr() with different method. If start position is multi-byte character's second byte or larger, it starts from first byte of multi-byte character.
It subtracts string from str that is shorter than length AND character that is not part of multi-byte string or not being middle of shift sequence.
For another example, there is a piece of text that is segmented using mb_substr and mb_strcut respectively:
PLAIN TEXT
CODE:
The code is as follows
|
Copy code
|
||||
$str = 'I am a relatively long string of Chinese-www.webjx.com'; echo "mb_substr:" . mb_substr($str, 0, 6, 'utf-8');
"; |
The code is as follows | Copy code |
/**
* String splitting by word
* @param $content string
* @param $length int
* @param $etc string
* @return string
*/
function Truncate($content, $length, $etc = '...') {
if ($length == 0) {
return '';
} elseif (mb_strlen($content,'utf-8') > $length) {
$length -= min($length, mb_strlen($etc));
$charset = 'utf-8';
$content = mb_substr($content, 0, $length, $charset) . $etc;
}
return $content;
}
$str ='Voltaire (1694~1778) French bourgeois Enlightenment thinker, philosopher, historian, and writer. Voltaire's original name was F.M. Arrouet. ';
echo strlen($str);//String length
echo ' '; Echo mb_strlen($str,'utf-8');//String length echo ' '; echo mb_strcut($str,0,35,'utf-8');//Split by bytes echo ' '; echo mb_substr($str,0,35,'utf-8');//Split by word echo ' '; echo Truncate($str,35);//String interception method |

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
