1. 字符串长度相关函数
strlen()
: 获取字符串长度
mb_strlen()
: 获取字符串长度(支持多字节字符)
2. 字符串查找相关函数
strpos()
: 查找字符串中是否包含另一个字符串,返回第一个匹配的位置
strrpos()
: 查找字符串中是否包含另一个字符串,返回最后一个匹配的位置
strstr()
: 查找字符串中是否包含另一个字符串,返回第一个匹配的部分及其后面的所有字符串
str_replace()
: 查找并替换字符串中的内容
preg_match()
: 使用正则表达式匹配字符串
3. 字符串截取相关函数
substr()
: 获取字符串的子串
mb_substr()
: 获取字符串的子串(支持多字节字符)
4. 字符串格式化相关函数
trim()
: 去除字符串两端的空格
strtolower()
: 将字符串转换为小写字母
strtoupper()
: 将字符串转换为大写字母
ucfirst()
: 将字符串的首字母转换为大写字母
ucwords()
: 将字符串中每个单词的首字母转换为大写字母
number_format()
: 格式化数字字符串
htmlspecialchars()
: 将特殊字符转换为HTML实体
示例代码:
// 使用substr截取字符串
$str = "Hello World!";
echo substr($str, 0, 5); // 输出:Hello
// 使用trim去除字符串两端的空格
$str = " Hello World! ";
echo trim($str); // 输出:Hello World!
// 使用strtolower将字符串转换为小写字母
$str = "Hello World!";
echo strtolower($str); // 输出:hello world!
// 使用strtoupper将字符串转换为大写字母
$str = "Hello World!";
echo strtoupper($str); // 输出:HELLO WORLD!
// 使用ucfirst将字符串的首字母转换为大写字母
$str = "hello world!";
echo ucfirst($str); // 输出:Hello world!
// 使用ucwords将字符串中每个单词的首字母转换为大写字母
$str = "hello world!";
echo ucwords($str); // 输出:Hello World!
// 使用number_format格式化数字字符串
$num = 12345.6789;
echo number_format($num, 2); // 输出:12,345.68
// 使用htmlspecialchars将特殊字符转换为HTML实体
$str = "Hello World!";
echo htmlspecialchars($str); // 输出:<strong>Hello World!</strong>
2023-05-01 19:12:50 更新