<?php
/*
* php string @mrwu
*/
$str=' let us learn php string';
//format
$str=trim($str);//clear blank
echo "after trim str=$str <br/>";
$str='this is <br/> a test';
$str=nl2br($str);//<br/> to /n
echo "after nl2br str=$str <br/>";
printf('my name is %s','jame green<br/>');
printf('u cost %.2f',2);
echo '<br/>';
echo strtoupper('jame'),strtolower('jame'),ucfirst('jame'),ucwords('jame'),'<br/>';
//format转义处理
$str="\//'jame/";
$str=addslashes($str);//" to /"
echo $str,'<br/>';
$str=stripslashes($str);// /" to "
echo $str,'<br/>';
//connect and cut
$str='1,2,3';
$arr=explode(',',$str);
$str=implode($arr,'-');//join() same as implode
echo $str,'</br>';
//strtok and substr()
$str=strtok($str,'-');
$str=strtok('-');
echo $str,'<br/>';//2
$str='my name is jame green';
$str=substr($str,2,-1);//3- last-2
echo $str.'<br/>';
//compare
$str1='123';
$str2='234';
$result=strcmp($str1,$str2);// = retunr 0,> return 1 else return -1
echo $result,'<br/>';
//strcasecmp ignore the case and strnatcmp follows nature sort
//find and replace
unset($str);
$str='my name is jame green';
$name=strstr($str,'j');//return jame green stristr ignore the case
echo $name,'<br/>';
$where=strpos($str,'j',11);//return 11 11 is the offset
echo $where,'<br/>';
$str=str_replace('jame','rory',$str);
echo $str,'<br/>';
$str=substr_replace($str,'xxxx',-2);
echo $str,'<br/>';
//reg
echo preg_match('/.at/','cat');
echo preg_match('/[a-z](very)/','bvery');
echo preg_match('/[^a-z](very){1,3}$/','2veryveryvery');
echo preg_match('/^(www.)[a-z]{3,6}.edu|com|net/','www.wo.com');