当需要在一个网站中展示PHP代码时,highlight_string()函数就变的非常有用了。该函数通过使用PHP语法高亮程序中定义的颜色,输出或返回给定的PHP代码的语法高亮版本。
示例:
1
2
3
|
<?php
highlight_string(
'<?php phpinfo(); ?>'
);
?>
|
该函数必须要传递一个参数,根据参数类型返回单词的个数。如下面的所示:
1
2
3
4
|
<?php
$str
=
"How many words do I have?"
;
echo
str_word_count
(
$str
);
//Outputs 6
?>
|
该函数主要返回两个字符串之间的Levenshtein距离。Levenshtein 距离,又称编辑距离,指的是两个字符串之间,由一个转换成另一个所需的最少编辑操作次数。许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。该函数对查找用户所提交的错别字非常有用。
示例:
1
2
3
4
5
|
<?php
$str1
=
"carrot"
;
$str2
=
"carrrott"
;
echo
levenshtein(
$str1
,
$str2
);
//Outputs 2
?>
|
该函数返回一个包含所有已定义变量列表的多维数组,这些变量包括环境变量、服务器变量和用户定义的变量。
示例:
1
|
print_r(get_defined_vars());
|
该函数用来避开字符串中的特殊符号,可以防止使用者耍花招来破解服务器系统。可以用本函数搭配exec() 或是system() 二个函数,这样可以减少网上使用者的恶意破坏行为。
示例:
1
2
3
4
5
|
<?php
$command
=
'./configure '
.
$_POST
[
'configure_options'
];
$escaped_command
=
escapeshellcmd
(
$command
);
system(
$escaped_command
);
?>
|
本函数可以用来检查日期是否有效,例如年为0至32767年、月为1至12月、日则随着月份及闰年变化。
示例:
1
2
3
4
5
6
7
|
<?php
var_dump(
checkdate
(12, 31, 2000));
var_dump(
checkdate
(2, 29, 2001));
//Output
//bool(true)
//bool(false)
?>
|
该函数可以返回已删除PHP注释以及空白字符的源代码文件,这对实际代码数量和注释数量的对比很有用。
示例:
1
2
3
4
5
6
7
8
9
|
<?php
// PHP comment here
/*
* Another PHP comment
*/
echo
php_strip_whitespace(
__FILE__
);
// Newlines are considered whitespace, and are removed too:
do_nothing();
?>
|
输出结果:
1
2
|
<?php
echo
php_strip_whitespace(
__FILE__
); do_nothing(); ?>
|
这里简单列举了几个函数的示例,函数的具体定义及说明请参考PHP手册
8. sys_getloadavg()
sys_getloadavt()可以获得系 统负载情况。该函数返回一个包含三个元素的数组,每个元素分别代表系统再过去的1、5和15分钟内的平均负载。与其让服务器因负 载过高而宕掉,不如在系统负载很高时主动die掉一个脚本,sys_getloadavg()就是用来帮你实现这个功能的。 不过很遗憾,该函数在windows下无效。
9. pack()
Pack() 能将md5()返回的32位16进制字符串转换为16位的二进制字符串,可以节省存储空间。
10. cal_days_in_month()
cal_days_in_month()能够返回指定月份共有多少天。
11. _()
WordPress开发者经常能见到这个函数,还有 _e()。这两个函数功能相同,与gettext()函数结合使用,能实现网站的多语言化。具体可参见PHP手册的相关部分介绍。
5. get_browser()
在发送页面前先看看用户的浏览器都能做些什么是 不是挺好?get_browser()能获得用户的浏览器类型,以及浏览器支持的功能,不过首先你需要一个php_browscap.ini文件,用来给 函数做参考文件。
要注意,该函数对浏览器功能的判断是基于该类浏览器的一般特性的。例如,如果用户关闭了浏览器对 JavaScript的支持,函数无法得知这一点。但是在判断浏览器类型和OS平台方面,该函数还是很准确的。
12. debug_print_backtrace()
这是一个调试用的函数,能帮助你发现代码中的逻辑错误。要理 解这个函数,还是直接看个例子吧:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
$a
= 0;
function
iterate() {
global
$a
;
if
(
$a
< 10 )
recur();
echo
$a
. “, “;
}
function
recur() {
global
$a
;
$a
++;
// how did I get here?
echo
“\n\n\n”;
debug_print_backtrace();
if
(
$a
< 10 )
iterate();
}
iterate();
|
# OUTPUT:
#0 recur() called at [C:\htdocs\php_stuff\index.php:8]
#1 iterate() called at [C:\htdocs\php_stuff\index.php:25]
#0 recur() called at [C:\htdocs\php_stuff\index.php:8]
#1 iterate() called at [C:\htdocs\php_stuff\index.php:21]
#2 recur() called at [C:\htdocs\php_stuff\index.php:8]
#3 iterate() called at [C:\htdocs\php_stuff\index.php:25]
#0 recur() called at [C:\htdocs\php_stuff\index.php:8]
#1 iterate() called at [C:\htdocs\php_stuff\index.php:21]
#2 recur() called at [C:\htdocs\php_stuff\index.php:8]
#3 iterate() called at [C:\htdocs\php_stuff\index.php:21]
#4 recur() called at [C:\htdocs\php_stuff\index.php:8]
#5 iterate() called at [C:\htdocs\php_stuff\index.php:25]
13. metaphone()
这个函数返回单词的metaphone值,相同读音的单词具有相同的metaphone值,也就是说这个函数可以帮你判断两个单词的读音是否 相同。
14. natsort()
natsort()能将一个数组以自然排序法 进行排列,直接看个例子吧:
1
2
3
4
5
|
$items
=
array
( “100 apples”, “5 apples”, “110 apples”, 55 apples”
);
// normal sorting:
sort(
$items
);
print_r(
$items
);
|
# Outputs:
# Array
# (
# [0] => 100 apples
# [1] => 110 apples
# [2] => 5 apples
# [3] => 55 apples
# )
1
2
|
natsort(
$items
);
print_r(
$items
);
|
# Outputs:
# Array
# (
# [2] => 5 apples
# [3] => 55 apples
# [0] => 100 apples
# [1] => 110 apples
# )
15. levenshtein()
Levenshtein()告诉你两个单词之间的“距离”。它告诉你如果想把一个单词变成另一个单词,需要插入、替换和删除多少字母。
看个例子吧:
1
2
3
4
5
6
7
8
9
10
11
12
|
$dictionary
=
array
( “php”, “javascript”, “css”
);
$word
= “japhp”;
$best_match
=
$dictionary
[0];
$match_value
= levenshtein(
$dictionary
[0],
$word
);
foreach
(
$dictionary
as
$w
) {
$value
= levenshtein(
$word
,
$w
);
if
(
$value
<
$match_value
) {
$best_match
=
$w
;
$match_value
=
$value
;
}
}
|
echo “Did you mean the ‘$best_match’ category?”;
16. glob()
glob()会让你觉得用 opendir(), readdir()和closedir()来寻找文件非常蠢。
1
|
foreach
(
glob
(“*.php”)
as
$file
)
echo
“
$file
\n”;
|