php字符串的应用
<?php
function &example3($stmp){
return $stmp;
}
$str=&example3("看到了!");
echo $str."<p>"; //对函数的引用
echo $str1='I’m a student'; //其中’为中文的
echo "I\'m a student";
$str3="I like play basketball";
echo substr($str3, 6)."</br>";
echo substr($str3, 1,7)."</br>"; //截取字符串
$text="茉莉花喜温暖湿润和阳光充足环境,在通风良好、半阴的环境生长最好。土壤以含有大量腐殖质的微酸性砂质土壤为最适合。大多数品种畏寒、畏旱,不耐霜冻、湿涝和碱土。冬季气温低于3℃时,枝叶易遭受冻害,如持续时间长就会死亡。";
if (strlen($text)>30) {
echo substr($text, 0,30);
}
else {
echo $text;
} //超长文本部分显示
//字符串的比较
$s1="message";
$s2="Message";
echo strcmp($s1, $s2)."</br>"; //按字节进行比较,区分大小写
echo strcasecmp($s1, $s2)."</br>"; //按字节比较,不区分大小写
echo strnatcmp($s1, $s2)."</br>"; //按自然排序法进行比较
echo strncmp($s1, $s2, 3)."</br>"; //按从源字符串的位置进行比较,str1==str2返回0,str1>str2返回大于0的数,str1<str2返回小于0的数
echo strncmp($s1, $s2, 0)."</br>";
$str4="This is beautiful flower";
echo strstr($str4, "beau")."</br>"; //检索字符串
$c=substr_count($str4, "l");
echo "l出现了:".$c."次</br>"; //检索子串出现的次数
$str5="girl";
echo str_ireplace("flower", $str5, $str4)."</br>"; //str5替换tr4中出现的"flower"字串,不区分大小写
$str6="car";
echo str_replace("flower", $str6, $str4)."</br>"; //str6替换tr4中出现的"flower"字串,区分大小写
$str7= "Let us go home.";
echo $str7."</br>"; ;
echo addcslashes($str7,'m')."</br>"; //在指定的字符前添加反斜杠
$str8 = "Who's John Adams?";
echo addslashes($str8)."</br>"; //在指定的预定义字符前添加反斜杠
//格式化字符串
$number=123456789;
echo number_format("$number")."</br>";
echo number_format("$number",2)."</br>";
echo number_format("$number",3,",",".")."</br>";
echo number_format("$number",2,"/",";")."</br>"; //参数:格式化字符串;小数的位数;小数的分隔符;千分位的分隔符。
$str9="我 的 梦 想 是 当 一 名 旅 游 者 !";
$str_arr=explode(" ",$str9,8);
print_r($str_arr);
echo "<br>";
echo '$str_arr[1]='.$str_arr[1]."</br>"; //字符串的分割
$arr10=array('Hello','welcome','to','my','house');
echo implode(" ",$arr10); //字符串的合并
?>
运行的结果:
看到了!
I’m a student
I\'m a student
play basketball
like p
茉莉花喜温暖湿润和阳光充足环境
1
0
1
1
0
beautiful flower
l出现了:2次
This is beautiful girl
This is beautiful car
Let us go home.
Let us go ho\me.
Let\'s go to school
123,456,789
123,456,789.00
123.456.789,000
123;456;789/00
Array
(
[0] => 我
[1] => 的
[2] => 梦
[3] => 想
[4] => 是
[5] => 当
[6] => 一
[7] => 名 旅 游 者 !
)
$str_arr[1]=的
Hello welcome to my house
浙公网安备 33010602011771号