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

Android Fragment及Activity 中使用sharedpreferences

Android Fragme...

在Android下想要儲存執行資料的方式有好幾種,例如存在手機空間或者是資料庫空間。 這裡紀錄一下使...

錯誤訊息Division by zero的解決方式

錯誤訊息Division b...

錯誤訊息Division by zero發生的原因是因為除數是導致的錯誤: 解決方式如下: ) 運行...

使用jquery判斷上傳檔案大小

使用jquery判斷上傳檔案...

首先得引用jquery var fileSize = document.getElementById...

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