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清空陣列函數

PHP清空陣列函數

pexelsphoto5555 unset(arrar) 將整個陣列清空,但之後如果再加入元素,其 ...

PHP使用array_product()函數求得陣列中的乘積

PHP使用array_pro...

arrayproduct()是PHP中的一個內建函數,可以返回陣列中所有元素的乘積。 例如 $arr...

PDO常用方法

pexelsphoto555.jpeg PDO::query() 主要用於有記錄結果返回的操作(PD...

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