PHP字符串操作函数
<?php /* * @author gf * 字符串操作函数 */ header("content-type:text/html;charset='utf-8'"); /* nl2br() 在字符串所有新行之前插入 HTML 换行标记 $str = "cat isn't dog"; $result = nl2br($str); echo $result; 结果 cat isn't dog */ /* rtrim() chop() 清除结尾的空白 ltrim() 清除开头的空白 trim() 清除开头结尾的空白 $str = ' hello,php '; $result = trim($str); echo $result; */ /* strip_tags() 清除字符串html和php的标记 $str = '<font color="blue">hello,phper</font>'; $result = strip_tags($str); echo $result; */ /* strtolower() 字符串转换成小写 strtoupper() 字符串转换成大写 $str = 'Hello ,PHPer'; $result = strtoupper($str); echo $result; */ /* str_repeat(string,int) 将一个字符串重复利用 $str = 'gaogaofeifei'; $result = str_repeat($str , 10); echo $result; */ /* str_replace(参数,参数,string) 区分大小写的替换 str_ireplace(参数,参数,string) 不区分大小写的替换字符串 $str = 'Gao fei'; $result1 = str_ireplace('gao','G',$str); $result2 = str_replace('fei','G',$str); echo $result1.'<br />'; echo $result2; */ /* str_word_count(string)返回字符串中单词的个数 str_word_count(string,int)返回一个数组 $str = 'james is a bad boy'; $result = str_word_count($str,1); $result2 = str_word_count($str); var_dump($result); echo $result2; */ /* strlen(string)返回字符串长度 $str = 'this is PHP!!!!'; $result = strlen($str); echo $result; */ /* substr_count(string,string)计算一个字符串在另一个字符串的个数 $str = 'ni hao , wo shi pinyin !!wo hahaha'; $result = substr_count($str , 'wo'); echo $result; */ /* substr_replace(string,参数,位数)从哪个位置开始替换字符串 $str = 'zhe shi php yu yan '; $result = substr_replace($str , 'yu' , 2); echo $result; */ /* substr(string,参数,参数)截取字符串 $str = 'this is php yuyan'; $result = substr($str , 0 , 2); echo $result; */ /* implode(参数,array) 将数组转换为字符串 join(参数,array) 将数组转换为字符串 explode(参数,string) 将字符串转为数组 $arr = array(1,2,3,4,5); $string = implode("/",$arr); echo $string; $str = '1,2,3,4,5'; $result = explode(",",$str); var_dump($result); */ /* md5()对字符串进行加密,多用于密码 php函数库内暂无MD5解密函数 crypt()对字符串进行加密 单项函数 产生字符串比较复杂不易破解 $str = 'goafei'; $result = md5($str); echo $result; */ /* * strtr()函数转换字符串中特定的字符。 $trans = array("hello" => "hi", "hi" => "hello"); echo strtr("hi all, I said hello", $trans); */ /* strstr()查找字符串的首次出现 $str = '82582785@qq.com'; $result = strstr($str,'@'); echo $result; */ /* strcmp() 二进字符串比较 $str1 = 'this is string!'; $str2 = 'this is new string! '; $result = strcmp($str2,$str1); if($result != 0) { echo '123'; } */ /* substr_count() $str = 'this is is is '; $result = substr_count($str,'is'); echo $result; */ /* json_encode()将数组转换为json数据 json_decode()将json数据转换为数组 $arr = array( '1' => 'gaofei', '2' => 'james' ); $json_data = json_encode($arr); var_dump($json_data); $array_data = json_decode($json_data); var_dump($array_data); */