| 
 | 
 
 
語法 : string substr (字串, 起始點 [, 擷取長度]) 
 
其中字串的起始點由0開始,可以為正數或負數, 
例如: 
字串 "abcdef"  , 0就是代表從 a 開始 ,-2代表從 e 開始 
 
擷取長度 則為起始點後的幾個字 -  
 
 - <?php
 
 -   $rest = substr("abcdef", -1);    // returns "f"
 
  
-   $rest = substr("abcdef", -2);    // returns "ef"
 
  
-   $rest = substr("abcdef", -3, 1); // returns "d"
 
  
- ?>
 
 
  複製代碼 英文數字切割只要用substr就綽綽有餘,那若有包含中文字呢??? 
以目前大部分網頁都是utf8為例,中文字為3個位元組,若混合英數符號, 
只有一個位元組的字,勢必在分隔文字會出現錯亂... 
 
 
psir這邊不私藏,分享兩個好用的函式提供有興趣的同好來使用, 
加入會員回覆後即可觀看 
 
1. function utf8Slices($string,$length=0,$start=0,$slices=false,$dotted_line=false ) 
$slices : true 返回字串  false  返回字數 
$dotted_line : true 過長自動加...  false  過長不自動加.... 
 
2. function cut_str($string, $sublen, $start = 0, $code = 'UTF-8') 
 
以上都為分割中文字的補強函式,請慢用 
 
  
 |  
  |   
 
 
 
 |