1.分割与合并
implode:
echo implode(",", array('lastname', 'email', 'phone'));//数组转成字符串
explode:
print_r(explode(",", 'lastname,email,phone'));//字符串转成数组
split:
print_r(split("[/.-]","2008-9.12"));//以/或.或-任一符号切成数组
str_split:
print_r(str_split("Hello Friend",1));//将字符串切开
preg_split:
//正则分割
//$ops = preg_split("{[+/-]}","3+59/2");
//print_r($ops);//返回:Array ( [0] => 3 [1] => 5 [2] => 9 [3] => 2 )
http_build_query:
//生成 url-encoded 之后的请求字符串
$data = array('localhost'=>'aa',
'user'=>'bb',
'password'=>'cc');
echo http_build_query($data);//返回:localhost=aa&user;=bb&password;=cc
strtok:
//将字符串切成小段
$string = "This is\tan example\nstring";
echo strtok($string,"\n\t");//返回:This is
echo '
preg_grep:
//返回与模式匹配的数组单元
$food = preg_grep("/^p/",array("apple","orange","pip","banana"));
print_r($food); //返回:Array ( [2] => pip )
strtr:
//以指定的数组替换找到的字符串
$arr = array("www"=>"ftp","yahoo"=>"baidu");
echo strtr("www.yahoo.com",$arr);//返回:ftp.baidu.com
echo strtr("www.yahoo.com","wo","sx");//返回:sss.yahxx.cxm 翻译字符串 把所有w换成了s把所有的o换成了x
strspn:
//找出比对到的最初部份的长度
echo strspn("abcdefg","1234567890");//返回:0
//找出没有比对到的最初部份的长度
echo strcspn("abcdefg","1234567890");//返回:7
3.正则
preg_match:
//返回 pattern 所匹配的次数。要么是 0 次(没有匹配)或 1 次,因为 preg_match() 在第一次匹配之后将停止搜索。
if (preg_match ("/php/i", "PhP is the web scripting language of choice."))
echo "存在";
else
echo "不存在";
preg_match_all:
//则相反,会一直搜索到 subject 的结尾处。
preg_match_all("/\(?(\d{3})?\)?(?(1)[\-\s])\d{3}-\d{4}/x",
"Call 555-1212 or 1-800-555-1212", $phones);
print_r($phones[0]);//取得所有的电话号码
ereg_replace:
//URL 替换为超连接
echo ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
"<a href=\"\\0\">\\0", '这是百度http://www.baidu.com网站。');
preg_replace:过滤
$search = array ("'<script[^>]?>.?'si", // 去掉 javascript
"'<[\/\!]?[^<>]?>'si", // 去掉 HTML 标记
"'([\r\n])[\s]+'", // 去掉空白字符
"'&(quot|#34);'i", // 替换 HTML 实体
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&#