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

將Google字型fonts.googleapis.com新增到CSP header 中

將Google字型fonts...

Content Security Policy (CSP) 內容安全政策 主要用來限制網頁中對外部的...

ajax錯誤訊息提示

ajax錯誤訊息提示

pexelsphoto55 ajax目前已經是我寫系統使用非常普遍的方式~ 雖說常寫,但偶爾還是會有...

修改php.ini更改允許上傳檔案的大小

修改php.ini更改允許上...

使用vi或vim開啟php.ini #vim /etc/php/apache/php.ini 找到以...

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