php常用的字符串转换

<?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;

 

 

 

 

 

 

 

 

?>

转载于:https://my.oschina.net/u/3433695/blog/912744

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值