PHP中的字符串操作函数列表
在PHP中,字符串操作非常常见,毕竟网站开发最后呈现给用户的就是字符串。因此,PHP提供了许多字符串操作函数,这些函数广泛应用于字符串处理中。本文将为大家列出PHP中的字符串操作函数列表。
1. strlen()
作用:返回字符串的长度。
语法:int strlen ( string $string )
示例:
<?php
$str = "Hello world!";
echo strlen($str); // 输出 12
?>
2. strpos()
作用:查找字符串中指定的字符或子串,如果找到返回位置从0开始,如果没找到返回false。
语法:int|string strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
示例:
<?php
$str = "Hello world!";
echo strpos($str,"world"); // 输出 6
?>
3. substr()
作用:从字符串中提取需要的子串。
语法:string substr ( string $string , int $start [, int $length ] )
示例:
<?php
$str = "Hello world!";
echo substr($str,6); // 输出 world!
?>
4. str_replace()
作用:替换字符串中的指定字符或子串。
语法:mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
示例:
<?php
$str = "Hello world!";
echo str_replace("world","PHP",$str); // 输出 Hello PHP!
?>
5. strtolower()
作用:将字符串转换为小写。
语法:string strtolower ( string $string )
示例:
<?php
$str = "HELLO WORLD!";
echo strtolower($str); // 输出 hello world!
?>
6. strtoupper()
作用:将字符串转换为大写。
语法:string strtoupper ( string $string )
示例:
<?php
$str = "hello world!";
echo strtoupper($str); // 输出 HELLO WORLD!
?>
7. ucwords()
作用:将字符串中每个单词的首字母大写。
语法:string ucwords ( string $str )
示例:
<?php
$str = "hello world!";
echo ucwords($str); // 输出 Hello World!
?>
8. str_word_count()
作用:计算字符串中单词的个数。
语法:int str_word_count ( string $string [, int $format = 0 [, string $charlist ]] )
示例:
<?php
$str = "hello world!";
echo str_word_count($str); // 输出 2
?>
9. strcmp()
作用:比较两个字符串。
语法:int strcmp ( string $str1 , string $str2 )
示例:
<?php
$str1 = "hello";
$str2 = "world";
echo strcmp($str1,$str2); // 输出 -5
?>
10. strrev()
作用:将字符串反转。
语法:string strrev ( string $string )
示例:
<?php
$str = "Hello world!";
echo strrev($str); // 输出 !dlrow olleH
?>
11. md5()
作用:对字符串进行MD5散列处理。
语法:string md5 ( string $str [, bool $raw_output = FALSE ] )
示例:
<?php
$str = "Hello world!";
echo md5($str); // 输出 b10a8db164e0754105b7a99be72e3fe5
?>
12. sha1()
作用:对字符串进行SHA1散列处理。
语法:string sha1 ( string $str [, bool $raw_output = FALSE ] )
示例:
<?php
$str = "Hello world!";
echo sha1($str); // 输出 0a4d55a8d778e5022fab701977c5d840bbc486d0
?>
13. htmlspecialchars()
作用:将特殊的字符转换为HTML实体。
语法:string htmlspecialchars ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = "UTF-8" [, bool $double_encode = TRUE ]]] )
示例:
<?php
$str = '<script>alert("Hello world!");</script>';
echo htmlspecialchars($str); // 输出 <script>alert("Hello world!");</script>
?>
14. htmlentities()
作用:将所有的字符转换为HTML实体。
语法:string htmlentities ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = "UTF-8" [, bool $double_encode = TRUE ]]] )
示例:
<?php
$str = '<script>alert("Hello world!");</script>';
echo htmlentities($str); // 输出 <script>alert("Hello world!");</script>
?>
15. nl2br()
作用:将换行符(
)转换为HTML中的<br>标签。
语法:string nl2br ( string $string [, bool $is_xhtml = true ] )
示例:
<?php
$str = "Hello
world!";
echo nl2br($str); // 输出 Hello<br />world!
?>
16. explode()
作用:将字符串分割成数组。
语法:array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] )
示例:
<?php
$str = "Hello world!";
$arr = explode(" ",$str);
print_r($arr);
/* 输出:
Array
(
[0] => Hello
[1] => world!
)
*/
?>
17. implode()
作用:将数组中的元素组合成字符串。
语法:string implode ( string $glue , array $pieces )
示例:
<?php
$arr = array("Hello","world!");
$str = implode(" ",$arr);
echo $str; // 输出 Hello world!
?>
18. trim()
作用:去除字符串首尾的空格或其他字符。
语法:string trim ( string $string [, string $character_mask = " \t
\r\0\x0B" ] )
示例:
<?php
$str = " Hello world! ";
echo trim($str); // 输出 Hello world!
?>
19. sprintf()
作用:将格式化的字符串输出。
语法:string sprintf ( string $format , mixed ...$args )
示例:
<?php
$name = "Tom";
$age = 20;
echo sprintf("%s is %d years old.",$name,$age); // 输出 Tom is 20 years old.
?>
20. preg_match()
作用:使用正则表达式匹配字符串。
语法:int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
示例:
<?php
$str = "Hello world!";
if(preg_match("/world/",$str)){
echo "匹配成功!";
}else{
echo "匹配失败!";
}
?>
以上列出的20个字符串操作函数,仅仅是PHP中的一部分,之后根据业务需求,开发者还可以自行掌握更多的字符串操作函数。无论你是PHP初学者还是资深开发者,都应该熟练掌握这些常用的字符串操作函数,它们对于PHP开发是很重要的基础知识。
