1. strlen():计算字符串的长度
strlen() 函数计算字符串的长度(以字符为单位)。
语法:strlen(string)
示例:
$str = "Hello, world!";
echo strlen($str); // 13
2. strpos():查找字符串中的子串
strpos() 函数在字符串中查找子字符串,并返回第一次匹配的位置。如果找不到该子串,则返回 false。
语法:strpos(string,substring)
示例:
```php
$str = "Hello, world!";
$pos = strpos($str, "world");
echo $pos; // 7
```
3. str_replace():替换字符串中的子串
str_replace() 函数将字符串中指定的子串替换为另一个子串。
语法:str_replace(search,replace,string)
示例:
```php
$str = "Hello, world!";
$new_str = str_replace("world", "PHP", $str);
echo $new_str; // Hello, PHP!
```
4. strtolower():将字符串转换为小写
strtolower() 函数将字符串中的所有字符转换为小写。
语法:strtolower(string)
示例:
```php
$str = "Hello, world!";
$new_str = strtolower($str);
echo $new_str; // hello, world!
```
5. strtoupper():将字符串转换为大写
strtoupper() 函数将字符串中的所有字符转换为大写。
语法:strtoupper(string)
示例:
```php
$str = "Hello, world!";
$new_str = strtoupper($str);
echo $new_str; // HELLO, WORLD!
```
6. substr():从字符串中取出一段子串
substr() 函数从字符串中取出一段子串,并返回该子串。
语法:substr(string,start,length)
示例:
```php
$str = "Hello, world!";
$substring = substr($str, 0, 5);
echo $substring; // Hello
```
7. trim():去除字符串首尾的空格
trim() 函数去除字符串首尾的空格。
语法:trim(string)
示例:
```php
$str = " Hello, world! ";
$new_str = trim($str);
echo $new_str; // Hello, world!
```
8. ltrim():去除字符串开头的空格
ltrim() 函数去除字符串开头的空格。
语法:ltrim(string)
示例:
```php
$str = " Hello, world! ";
$new_str = ltrim($str);
echo $new_str; // Hello, world!
```
9. rtrim():去除字符串结尾的空格
rtrim() 函数去除字符串结尾的空格。
语法:rtrim(string)
示例:
```php
$str = " Hello, world! ";
$new_str = rtrim($str);
echo $new_str; // Hello, world!
```
10. ucfirst():将字符串的首字母转换为大写
ucfirst() 函数将字符串的首字母转换为大写。
语法:ucfirst(string)
示例:
```php
$str = "hello, world!";
$new_str = ucfirst($str);
echo $new_str; // Hello, world!
```
11. ucwords():将字符串中每个单词的首字母转换为大写
ucwords() 函数将字符串中每个单词的首字母转换为大写。
语法:ucwords(string)
示例:
```php
$str = "hello, world!";
$new_str = ucwords($str);
echo $new_str; // Hello, World!
```
12. sprintf():格式化字符串
sprintf() 函数将字符串按照指定格式进行格式化,并返回格式化后的字符串。
语法:sprintf(format,arg1,arg2,...)
示例:
```php
$price = 12.34;
$quantity = 5;
$message = sprintf("You ordered %d items at $%.2f each.", $quantity, $price);
echo $message; // You ordered 5 items at $12.34 each.
```
13. nl2br():在字符串中的换行符后添加 HTML 换行标签
nl2br() 函数在字符串中的换行符后添加 HTML 换行标签。
语法:nl2br(string)
示例:
```php
$str = "Hello,\nworld!";
$new_str = nl2br($str);
echo $new