php函数输出反字符串,PHP | 字符串输出和格式化函数

PHP | 字符串输出和格式化函数

PHP | 字符串输出和格式化函数

字符串函数:

1.字符串的处理介绍

定义:$str='hello world!'    $str="hello world!" :双引号可以解析变量

输出:

echo $str;

print $str;

printf('--%s--',$str);

sprintf('--%s--',$str);

8c176846b1e1c5431021be07f6d01946.png

连接:

b43b352e4cc232bce047b6cdac5bc72c.png

2.常用的字符串函数

1)去除空格和字符串填补函数

ltrim()-------------------------------------------消除字符串中的左侧空格

$str=" asdfg";//左边敲了5个空格

echo strlen($str).'-'.$str;//10- asdfg

$str=ltrim($str);//消除字符串中的左侧空格

echo strlen($str).'-'.$str;//5- asdfg

?>

rtrim()------------------------------------------消除字符串中的右侧空格

trim()------------------------------------------消除字符串中的两侧空格

str_pad()---------------------------------------向字符串填补内容

$str="asdfg";//左边敲了5个空格

echo str_pad($str, 10, '-');//asdfg-----

//将字符串$str补为10个长度,剩下用-补齐

?>

str_repeat()---------------------------------------重复字符串的内容

echo str_repeat('as', 30);//将字符串as重复30次

//asasasasasasasasasasasasasasasasasasasasasasasasasasasasasas

?>

2)字符串大小写转换函数

strtolower()-------------------------------------转换为小写

$str='HELLO WORLD!';

echo strtolower($str);//hello world!

?>

strtoupper()-------------------------------------转换为大写

ucfirst()-------------------------------------首字母大写

$str='hello world!';

echo ucfirst($str);//Hello world!

?>

ucwords()--------------------------------------每个单词首字母大写

$str='hello world!';

echo ucwords($str);//Hello World!

?>

3)与HTML相关联的字符串函数

nl2br()-------------------------把\n换行符换成br标签

$str="aaaaaa\nbbbb\ncccccccc\n";

echo $str;

//aaaaaa bbbb cccccccc

echo nl2br($str);

/*

aaaaaa

bbbb

cccccccc

*/

?>

strip_tags()-------------------------过滤HTML标签代码

见下图

htmlspecialchars()----------------------将字符串转实体

见下图

7f1f7bb1d1423e71ef30b4dc2fe1a37c.png

78fc4b88ec0afbf36e5281fe1564c73a.png

3.常用的字符串格式化函数

strrev()--------------------字符串翻转string reverse

$str='123456';

echo strrev($str);//654321

?>

number_format()----------------------通过千位分组来格式化数字

$str='123456154665421';

echo number_format($str)."
";//123,456,154,665,421

echo number_format($str,2)."
";//123,456,154,665,421.00

echo number_format($str,2,",",".");//123.456.154.665.421,00

/* number_format(number,decimals【规定小数点位数】,

decimalpoint【规定用作小数点的字符串】,separator【规定用作千位分隔符的字符串】) */

?>

md5() ------------------------------计算字符串的 MD5 散列

$str='i like php a lot';

echo md5($str);//6c72ed5ed832a76dedbb52a8a194087b

?>

str_shuffle()------------------------随机地打乱字符串中的所有字符

$str='i like php a lot';

echo str_shuffle($str);//holip k ite pla

?>

PHP | 字符串输出和格式化函数相关教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值