Loading...

使用PHP mb_substr() 函式,擷取部分字串並記得使用UTF-8。

Temperature: 0 °C

Mark ChangMark Chang
author_tools


使用PHP mb_substr() 函式,擷取部分字串並記得使用UTF-8。
文字長度太長,一般來說我們可以使用DIV包住字串,再定義DIV的最大高度及最大寬度。

然後使用hidden的方式讓超出範圍的字串隱藏起來~

當然還可以使用以下函式:

PHP mb_substr() 函式基本語法

string mb_substr ( string $str , int $start , int $length , string $encoding )

mb_substr 是用來取得部分字串的函式,用法與 substr() 類似,但 mb_substr 多了編碼的判斷。

若是中文字串最好能使用UTF-8編碼。

以下使用UTF-8,PHP mb_substr() 函式範例說明
$string="我家門前有小河and後面有山坡";
 echo mb_substr( $string,0,10,"utf-8");


在上列範例中有個字串叫做「我家門前有小河and後面有山坡」。

參雜了中文字與英文字,再使用 mb_substr 截取字串。

從第 0 個字擷取到第 10 個字,並採用 UTF-8 編碼。

這樣跑出來的結果就是「我家門前有小河and」這 10 個字。
 

https://innstory.com/story-使用PHP_mbsubstr_函式擷取部分字串並記得使用UTF8-1642
PHP

Prev
 jQuery_resize取得瀏覽器目前視窗寬度

Next
分享_涉貪想落跑門都沒有!馬來西亞前總理納吉遭限制出境印尼度假計畫泡湯風傳媒 

About the Author

Mark Chang

離不開電腦的宅男

Visitor message

Leave some footprints to prove that you visited me

Recommended reading

Author's other related stories

在PHP裡添加HTTP Strict Transport Security

在PHP裡添加HTTP St...

要通過 PHP 啟用 HTTP 嚴格傳輸安全,請使用以下設定。 <?php //告訴瀏覽器站點只能通...

讓文字左右對齊

讓文字左右對齊

讓文字左右對齊這類的設計案,通常都會出現在平面設計居多。 網頁上很少會遇到客戶會執著於這方面,但不能...

PHP Caching Headers

PHP Caching He...

pexelsphotospeed 最近測試平台系統執行速度,老實說測試的有點無力~ 總覺得能做的可以...

Please select an option

error

Hi, thank you for your participation, but you cannot vote repeatedly~

Join innstory now and start recording your story.

"Innstory" is a place to store stories. We are committed to becoming a warm platform. Deepening the bonds between people is our direction.
We are convinced that the blockchain between people is not just a cold calculation. Join us now.

Wrong format