一天一篇之php学习篇2_1

php字符串操作

1单引号和双引号的区别

<?php
$test = “PHP”;
$str = “I love $test”;
$str1 = ‘I love $test’;
echo $str; //输出I love PHP
echo $str1; //输出I love $test
?>

双引号的内容是经过php语法分析器解析过的,任何变量在双引号中都会转换成它的值输出,而单引号的内容则是所见及所得的,无论有无变量,都会当成普通的字符串直接原样输出

2去除字符串首位空格和特殊字符 trim(),ltrim(),rtrim(),获取字符串长度strlen(),截取字符串substr(),比较字符串strcmp(),strcasecmp(),检索字符串strstr(),替换字符串substr_replace(),格式化字符串number_format(),分隔字符串explode()

<?php
$str = " acb     ";
echo trim($str,' ');//acb
echo strlen($str);//6
echo substr($str,0,2);
echo strstr($str," ");//acb
echo explode($str,' ');//array
?>

 

 

 

posted @ 2014-01-11 14:08  独步寻花  阅读(124)  评论(0编辑  收藏  举报