小白兔
大黑狼

<?php

//1.字符串输出

$str1 = "hello world";

//输出一个字符串

echo $str1."<hr>";

$str2 = "hehe";

//输出多个字符串

echo $str1,$str2,"nishihaoren","haorenzaina";

//格式化输出 (数字) printf

echo "<hr>";

printf("%.2f",3.6415966);保留两位小说(四舍五入)

printf("%d",3.1415);取整数部分

 

//2.字符串替换

//查找

$index = strpos($str1, "h");返回查找到的下标

echo "<hr>".$index;

 

//替换 str_reolace(find,replace,string,count);

 

/*

* find 必需 需要被替换的

* replace 必需 替换后是谁

* string 必需 要操作的字符串

* count 可选 对替换进行计数的变量

*/

echo "<hr>";

 

$res = str_replace("l", "i", $str1,$count);

echo $res;

    echo "<hr>";

echo $count;

 

//字符串截取 substr()

//参数1:要截取的字符串    

//参数2:截取的起始位置

//参数3:截取几个字符

echo "<hr>";

$str3 = "woshihaoren";

$res = substr($str3,5);haoren

echo $res;

 

echo "<hr>";

$res = substr($str3,5,3);hao

echo $res;

 

echo "<hr>";

$res = substr($str3,-5);aoren

echo $res;

 

echo "<hr>";

$res = substr($str3,-5,3);aor 从倒数第五个开始,查3个字符

echo $res;

 

echo "<hr>";

$res = substr($str3,-5,-3);ao 从倒数第五个截取到倒数第三个

echo $res;

 

echo "<hr>";

$res = substr($str3,5,-3);hao 从下标为5的位置,截取到倒数3个

echo $res;

 

//从字符开始截取 strstr()

echo "<hr>";

$res = strstr($str3,"h");

echo $res;

echo "<hr>";

 

//字符串删除

echo "<hr>";

$res = str_replace("hao", "",$str3);

echo $res;

 

echo "<hr>";

$str4 = "        woshihaoren      ";

$res = trim($str4);

echo "a".$str4."b";

 

//字符串长度 strlen()

echo "<hr>";

echo "字符串长度是".strlen($str3);

 

//字符串比较

echo "<hr>";

$res = strcmp("a", "A"); 比较返回的是ASCII差值,前减后

echo $res;

 

//字符串反转  strrev

echo "<hr>";

$res = strrev('woshi');

echo $res;

 

//字符串转数组 explode

echo "<hr>";

$str5 = "wo shi hao ren he he";

$res = explode(" ", $str5);

print_r($res);

 

//数组转字符串 implode

echo "<hr>";

$res2 = implode("",$res);

echo $res2;

 

 

 

 

 

 

 

 

?>

posted on 2017-06-01 17:46  撒了一地  阅读(463)  评论(0)    收藏  举报