PHP中不常见但有用的十个函数
1. array_key_first() 和 array_key_last()
在 PHP 7.3 中引入的 array_key_first() 和 array_key_last() 函数可以用于获取数组中的 个和最后一个键名。这两个函数对于需要特定键名的场景非常有用。
示例:
$colors = [
'red' => '#FF0000',
'green' => '#00FF00',
'blue' => '#0000FF'
];
$first_color = array_key_first($colors); // 'red'
$last_color = array_key_last($colors); // 'blue'
2. preg_replace_callback_array()
preg_replace_callback_array() 函数在 PHP 7.0 中引入,它允许我们使用多个回调函数来替换一个正则表达式匹配到的内容。这样做可以使代码更加可读和易于维护。
示例:
$text = "Hello, world! It's a beautiful day.";
$patterns = [
'/\bHello\b/' => function($match) { return 'Hi'; },
'/world/' => function($match) { return 'planet'; },
'/beautiful/' => function($match) { return 'gorgeous'; }
];
$new_text = preg_replace_callback_array($patterns, $text);
echo $new_text; // Hi, planet! It's a gorgeous day.
3. str_contains()
str_contains() 函数在 PHP 8.0 中引入,可以用于判断一个字符串是否包含另一个字符串。这个函数比较实用,以前需要写一大堆代码才能实现同样的功能。
示例:
$name = 'John Smith';
if (str_contains($name, 'Smith')) {
echo 'The name contains "Smith".';
}
4. str_starts_with() 和 str_ends_with()
str_starts_with() 和 str_ends_with() 函数在 PHP 8.0 中引入,可以分别用于判断一个字符串是否以另一个字符串开头或结尾。这两个函数也比较实用。
示例:
$url = 'https://www.example.com/';
if (str_starts_with($url, 'https://')) {
echo 'The URL is secure.';
}
$filename = 'example.txt';
if (str_ends_with($filename, '.txt')) {
echo 'The file is a text file.';
}
5. array_replace_recursive()
array_replace_recursive() 函数可以递归地合并两个或多个数组,其中后一个数组将覆盖前一个数组中相同键名的值。这个函数对于需要合并多个多维数组的场景非常有用。
示例:
$colors1 = [
'red' => [
'shade' => '#FF5555',
'name' => 'fire brick'
],
'green' => [
'shade' => '#00FF00',
'name' => 'lime'
]
];
$colors2 = [
'red' => [
'shade' => '#FF0000',
'name' => 'red'
]
];
$merged_colors = array_replace_recursive($colors1, $colors2);
print_r($merged_colors);
/*
输出结果:
Array
(
[red] => Array
(
[shade] => #FF0000
[name] => red
)
[green] => Array
(
[shade] => #00FF00
[name] => lime
)
)
*/
6. strtok()
strtok() 函数可以解析一个字符串并返回指定的分隔符之前的部分。这个函数对于处理需要对字符串进行再处理的情况非常有用。
示例:
$url = 'https://www.example.com/path/to/page';
$protocol = strtok($url, ':');
$hostname = strtok('/');
echo $protocol; // 'https'
echo $hostname; // 'www.example.com'
7. array_column()
array_column() 函数可以从一个多维数组中获取指定列的值,并返回一个一维数组。这个函数对于从数据库中获取一列数据的场景非常有用。
示例:
$users = [
['name' => 'John', 'age' => 35],
['name' => 'Mary', 'age' => 25],
['name' => 'Bob', 'age' => 45]
];
$names = array_column($users, 'name');
print_r($names);
/*
输出结果:
Array
(
[0] => John
[1] => Mary
[2] => Bob
)
*/
8. htmlspecialchars_decode()
htmlspecialchars_decode() 函数可以将 HTML 实体转换回其原始字符。这个函数对于从数据库中获取已经被转义的 HTML 内容的场景非常有用。
示例:
$html = '<p>This is a <b>bold</b> text.</p>'; $decoded_html = htmlspecialchars_decode($html); echo $decoded_html; // '<p>This is a <b>bold</b> text.</p>'
9. parse_str()
parse_str() 函数可以将 URL 参数解析为变量和值的数组。这个函数对于从 URL 中获取变量的值的场景非常有用。
示例:
$url = 'https://www.example.com/?name=John&age=35&occupation=programmer'; parse_str(parse_url($url, PHP_URL_QUERY), $params); echo $params['name']; // 'John' echo $params['age']; // '35' echo $params['occupation']; // 'programmer'
10. array_walk_recursive()
array_walk_recursive() 函数可以递归地遍历多维数组,并对每个元素执行指定的回调函数。这个函数对于需要对多维数组进行特定处理的场景非常有用。
示例:
$fruits = [
'apple',
'banana',
['orange', 'lemon'],
'grape'
];
$add_s_callback = function(&$value, $key) {
$value .= 's';
};
array_walk_recursive($fruits, $add_s_callback);
print_r($fruits);
/*
输出结果:
Array
(
[0] => apples
[1] => bananas
[2] => Array
(
[0] => oranges
[1] => lemons
)
[3] => grapes
)
*/
总结:
以上是 PHP 中不常见但有用的十个函数,它们可以帮助我们更加高效地编写代码,提高开发效率。这些函数中有些是新的,需要 PHP 7.3 或 PHP 8.0 或以上版本才能使用。所以,编写 PHP 代码时需要注意选择合适的函数和版本。
