PHP中提供許\多分割與合併字串的函式,以下就來瀏覽一下吧! 
  
1.explode():此函式可以將字串分割為陣列儲存,類似切token的方式,若以下列程式碼為例:  
$student=\"kevin susan james\"; 
$stuednt_array=explode(\" \",$student); 
foreach($stuednt_array as $index => $value) 
echo \"student $index is: $value\\n\";  
  
則結果為:
  
student 0 is: kevin
  
student 1 is: susan
  
student 2 is: james
  
  
2.implode():此函式可以將陣列合併為字串儲存,若以下列程式碼為例:
  
$stuednt_array = array(kevin, susan, james); 
$student = implode(\",\", $stuednt_array); 
echo $student  
  
則結果為:
  
kevin,susan,james
  
  
3.join():此函式和implode()用法相同,不再舉例。
  
  
  
4.split():這個函式和implode()很相像,不同的是他可以用regular expression,先看他的官方文件:
  
array split ( string $pattern , string $string [, int $limit ] )  
  
而官方的舉例如下:
  
$date = \"04/30/1973\"; 
list($month, $day, $year) = split(\'[/.-]\', $date); 
echo \"Month: $month; Day: $day; Year: $year\\n\";  
  
依此例子,則會印出以下結果:
  
Month: 04; Day: 30; Year: 1973
  
5.str_split():此函式會將字串以字母切割,並儲存成陣列,先看他的官方文件。
  
array str_split ( string $string [, int $split_length ] )  
  
舉例而言:
  
$str = \"Hello\"; 
$arr1 = str_split($str); 
print_r($arr1);  
  
印出結果會如下:
  
Array
  
(
  
   [0] => H
  
   [1] => e
  
   [2] => l
  
   [3] => l
  
   [4] => o
  
)
  
 
 
 
 
 
 
$string  = \"11 22 33 44 55 66\"; 
 
// \" \" 為要切割的基準點 
$output = explode(\" \", $string); 
 
 
echo $output[0];      // 11 
echo $output[1];      // 22 
echo $output[2];      // 33 
echo $output[3];      // 44 
echo $output[4];      // 55 
echo $output[5];      // 66 
 
 
 
 
 
 
 
 
 
 
 
$orginal_string = \"今天天氣非常好\"; 
$result = mb_substr($orginal_string, 0, 3, \'BIG-5\'); 
echo $result; // 會輸出 \"今天天\" 
?> 
 
 
 
$string = \'這是測試用的中文字\'; 
$string = mb_substr($string, -1, 3, \'BIG-5\'); 
echo $string;  // 會輸出 \'中文字\' 
?>  
 
  |