PHP字符串相关的方法

//1.去掉空格及特殊字符 trim函数

//$str='asdf ghjkl\t';

//echo($str);
//trim 去掉两端空格

//echo(trim($str));
//echo('test');

//移除两端指定的字符 as 和 \t
//echo(trim($str,'as\t'));

//$str1=' asdfg';
//移除左边的空格
//echo(ltrim($str1));

//$str2='lamco.com.cn';
//
//echo(ltrim($str2,'lam'));

//$str3='www.lamco.com.cn';
//移除右边空格
//echo(rtrim($str3));
//echo(rtrim($str3,'.cn'));

//2.获取字符串长度

//$str3='hello lamco';
//echo(strlen($str3));

//3.截取字符串
//$str4='http://www.lamco.com.cn';

//echo(substr($str4,0,4));
//从指定索引位置开始截取字符串 到字符串结尾
//echo(substr($str4,7));

//echo(substr($str4,-5));

//从字符串的末尾开始算起,截取的倒数第二位位置
//echo(substr($str4,-5,-2));


//4.比较字符串

//$str1='lisi1';

//$str2 = 'lisi';

//-1 0 1

//echo(strcmp($str1,$str2));

//$str1='10';
//$str2='2';
//按照字母的出现顺序比较 不区分大小写
//echo(strcasecmp($str1,$str2));

//按照自然数大小比较
//echo(strnatcmp($str1,$str2));

//$str3 ='asdff';
//$str4='asdfghj';

//比较前几个字符
//echo(strncmp($str3,$str4,3));

//查找字符串

//$str = 'http://www.welcome to lamco.com.cn';
//返回复合条件的字符串到结尾的所有字符
//echo(strstr($str,'www'));

//$str ='asdfghgfdsadasdf';
//检索指定字符串在字符串中出现的次数
//echo(substr_count($str,'a'));

//$str='welcome to sdzysdzysdzy';

//查找指定字符串中需要替换的字符,
//1.要替换的字符
//2.替换后的字符
//3.要查询的字符串
//4.替换字符的次数

//echo(str_ireplace('sdzy','lamco.com.cn',$str,$count));
//echo($count);

//替换字符串

//$str='welcome to lamco.com.cn';
//从指定索引位置替换指定长度的字符串
//echo(substr_replace($str,'lamco',0,7));

//字符串格式化
//echo(number_format(123456));

//echo(number_format(123456,3));

//echo(number_format(123456,3,',','.'));

//分割字符串

//$str ='a+s+d+f+g+h+k';
//
//$arr= explode('+',$str);
//
//foreach($arr as $item)
//{
// echo($item.' ');
//}

//合成字符串
//$newStr= implode('_',$arr);
//echo($newStr);

posted @ 2016-03-30 21:30  清浅晨曦  阅读(171)  评论(0编辑  收藏  举报