9.3.4 字符串的分割與連接(explode、implode join、preg_split)
2.php
<?php
/* $reg = '/(https?|ftps?):\/\/(www|mail|bbs|ftp)\.(.*?)\.(net|com|org|cn)([\w-\.\/\=\?\&\%]*)?/';
$reg = '/\w+([+-.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* /i';
* 分割、匹配、查找、替換
*
* 1. 字符串處理函數 (處理快, 但有一些做不到)
*
* 2. 正則表達式函數 (功能強大,但效率要低)
*
*
* 注意:如果可以直接使用字符串處理函數處理的字符串,就不要使用正則處理
*
*
*
* 匹配查找:
*
* strstr strpos substr
*
*
* 正則匹配查找
*
* preg_match() preg_match_all() preg_grep();
*
*
* 分割:
* explode() implode() -- join()
*
*
*正則表達式
preg_split()
*
*
*/
header("Content-Type:text/html;charset=utf-8");
$str = "this is a test.
hello word,
ni hao.
";
// print_r( explode("mn", $str, 3) );
print_r( preg_split('/[.,!? ]/', $str, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_OFFSET_CAPTURE ) );
test.php
<?php
/* $reg = '/(https?|ftps?):\/\/(www|mail|bbs|ftp)\.(.*?)\.(net|com|org|cn)([\w-\.\/\=\?\&\%]*)?/';
$reg = '/\w+([+-.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* /i';
* 分割、匹配、查找、替換
*
* 1. 字符串處理函數 (處理快, 但有一些做不到)
*
* 2. 正則表達式函數 (功能強大,但效率要低)
*
*
* 注意:如果可以直接使用字符串處理函數處理的字符串,就不要使用正則處理
*
*
*
* 匹配查找:
*
* strstr strpos substr
*
*
* 正則匹配查找
*
* preg_match() preg_match_all() preg_grep();
*
*
* 分割:
* explode() implode() -- join()
*
*
*正則表達式
preg_split()
*
*
*/
header("Content-Type:text/html;charset=utf-8");
$str = "lamp";
// print_r( explode("mn", $str, 3) );
$arr=preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
echo implode("++++",$arr)."<br>";
list($a, $b) = explode("_", "mei_zi");
echo $a."<br>";
echo $b."<br>";