1、概述
将字符串视为数组进行操作
将字符串作为数组(集合)处理是,对字符串元素(数组元素)的访问可以使用字符串名【】的形式,也可以使用字符串名{ }的形式,方括号向下兼容,{ }向上兼容,但浏览器提示是反的、
2、常用字符串数组函数
1、echo
略
2、print()函数
成功返回1,失败返回0,效率没有echo高
3、die()
exit函数的别名,在退出时输出字符
4、printf()格式化输出
%作为格式化输出的格式修饰符
如%d,%%,%e
5、sprintf()
类似printf,吧格式化输出的的字符输出到变量中去,从而便于使用
3、常用的字符串格式化函数
1、去除空格
<?php
$str=" hello world ";
echo "---".trim($str)."---";
echo "<br>";
echo "---".ltrim($str)."---";
echo "<br>";
echo "---".rtrim($str)."---";
echo "<br>";
?>
2、字符串大小写转换
<?php
$str="hello world";
echo strtoupper($str);
echo "<br>";
echo strtolower($str);
echo "<br>";
echo ucfirst($str);
echo "<br>";
echo ucwords($str);
echo "<br>";
?>
4、和HTML标签相关的字符串格式化
1、nl2br()函数
把\n转化为br
<?php
$str="hello\nworld";
echo nl2br($str);
?>
2、htmlspecialchars($string)
将html特殊字符转化为html实体
略
5、其他字符格式化函数
1、strrev()反转字符
<?php
$str="hello\nworld";
echo strrev($str);
?>
2、number_format()函数
<?php
$number=123456789;
echo number_format($number);
echo "<br>";
echo number_format($number,2);
echo "<br>";
echo number_format($number,2,",",".");
?>
3、MD5()函数
<?php
$password="hello world";
echo md5($password);
echo "<br>";
if(md5($password)=='5eb63bbbe01eeed093cb22bb8f5acdc3'){
echo "密码一致,登录成功";
}
?>
6、字符串比较函数
可以使用比较运算符比较,也可以使用
1、strcasecmp(str1,str2)忽略大小写按字节顺序比较
相等:返回0;
str1>str2:返回1
str1<str2:返回-1
<?php
$pws1="hello world";
$pws2="hello moto";
$pws3="HELLO WORLD";
echo strcmp($pws1, $pws2);
echo "<br>";
echo strcasecmp($pws1, $pws3);
?>
2、strnatcmp()自然排序进行比较
不想知道