首頁 php教程 php手册 浅析PHP substr,mb_substr以及mb_strcut的区别和用法

浅析PHP substr,mb_substr以及mb_strcut的区别和用法

Jun 13, 2016 am 11:46 AM
php substr 函數 分裂 差別 可以 用法

PHP substr()函数可以 分割文字,但要分割的文字如果包括中文字符往往会遇到问题,这时可以用mb_substr()/mb_strcut这个函数,mb_substr() /mb_strcut的用法与substr()相似,只是在mb_substr()/mb_strcut最后要加入多一个参数,以设定字符串的编码,但是 一般的服务器都没打开php_mbstring.dll,需要在php.ini在把php_mbstring.dll打开。
举个例子:

复制代码 代码如下:


echo mb_substr('这样一来我的字符串就不会有乱码^_^', 0, 7, 'utf-8');
?>


输出:这样一来我的字

复制代码 代码如下:


echo mb_strcut('这样一来我的字符串就不会有乱码^_^', 0, 7, 'utf-8');
?>


输出:这样一
从上面的例子可以看出,mb_substr是按字来切分字符,而mb_strcut是按字节来切分字符,但是都不会产生半个字符的现象……
mbstring 函数的说明:
php的mbstring扩展模块提供了多字节字符的处理能力,平常最常用的就是用mbstring来切分多字节的中文字符,这样可以避免出现半个字符的情况,由于是php的扩展,它的性能也要比一些自定义的多字节切分函数要好上一些。
mbstring extension提供了几个功能类似的函数,mb_substr和mb_strcut,看看手册上对它们的解释。

复制代码 代码如下:


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 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.


再举个例子,有一段文字, 分别用mb_substr和mb_strcut来做切分:
PLAIN TEXT
CODE:

复制代码 代码如下:


$str = '我是一串比较长的中文-www.webjx.com';
echo "mb_substr:" . mb_substr($str, 0, 6, 'utf-8');
echo "
";
echo "mb_strcut:" . mb_strcut($str, 0, 6, 'utf-8');
?>


输出结果如下:
mb_substr:我是一串比较
mb_strcut:我是
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 Dec 24, 2024 pm 04:42 PM

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 Dec 20, 2024 am 11:31 AM

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發

deepseek r1版本和v3版本有什麼區別 deepseek r1版本和v3版本有什麼區別 Feb 19, 2025 pm 03:24 PM

deepseek r1版本和v3版本有什麼區別

比特幣有沒有股票?比特幣有股權嗎? 比特幣有沒有股票?比特幣有股權嗎? Mar 03, 2025 pm 06:42 PM

比特幣有沒有股票?比特幣有股權嗎?

DeepSeek使用常見問題匯總 DeepSeek使用常見問題匯總 Feb 19, 2025 pm 03:45 PM

DeepSeek使用常見問題匯總

您如何在PHP中解析和處理HTML/XML? 您如何在PHP中解析和處理HTML/XML? Feb 07, 2025 am 11:57 AM

您如何在PHP中解析和處理HTML/XML?

php程序在字符串中計數元音 php程序在字符串中計數元音 Feb 07, 2025 pm 12:12 PM

php程序在字符串中計數元音

盤前盤後交易有什麼區別?盤前盤後交易區別詳解 盤前盤後交易有什麼區別?盤前盤後交易區別詳解 Mar 03, 2025 pm 11:54 PM

盤前盤後交易有什麼區別?盤前盤後交易區別詳解

See all articles