Heim  >  Artikel  >  Backend-Entwicklung  >  7个鲜为人知却超级有用的PHP函数_PHP教程

7个鲜为人知却超级有用的PHP函数_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:38:58733Durchsuche

PHP有着众多的内置函数,其中大多数函数都被开发者广发使用。但也有一些同样有用却被遗忘在角落,本文将介绍7个鲜为人知功能却非常酷的函数。

1.highlight_string()

当需要在网页中展示PHP代码时,highlight_string()函数就显得非常有用。该函数通过PHP内置定义的颜色,返回函数中代码的高亮显示版本。

Usage:

<?php
highlight_string(&#39;<?php phpinfo(); ?>&#39;);
?>
2.str_word_count()

这个函数可以方便的将输入的字符串参数中的单词个数返回。

Usage:

?php
$str = "How many words do I have?";
echo str_word_count($str); //Outputs 5
?>
3.levenshtein()

当需要跟踪用户的拼写错误时,该函数可以方便的返回两个参数之间的levenshtein(编辑距离)。

Usage:

<?php
$str1 = "carrot";
$str2 = "carrrott";
echo levenshtein($str1, $str2); //Outputs 2
?>
4.get_defined_vars()

这个函数在调试程序的时候非常有用,它会返回包含所有已定义变量的数组,其中包含环境、系统以及用户自定义变量。

Usage:

print_r(get_defined_vars());

5.escapeshellcmd()

该函数用来跳过字符串中的特殊符号,防止恶意用户耍花招破解服务器系统。可以搭配exec()与system()函数使用。

Usage:

<?php
$command = &#39;./configure &#39;.$_POST[&#39;configure_options&#39;];

$escaped_command = escapeshellcmd($command);
 
system($escaped_command);
?>
6.checkdate()

该函数可以用来检测日期参数的有效性。它可以验证输入的每一个参数的合法性。


Usage:

<?php
var_dump(checkdate(12, 31, 2000));
var_dump(checkdate(2, 29, 2001));

//Output
//bool(true)
//bool(false)
?>
7.php_strip_whitespace()

该函数会返回删除了注释与空格后的PHP源码。这对实际代码数量和注释数量的对比很有用。

Usage:

<?php
// PHP comment here

/*
 * Another PHP comment
 */

echo        php_strip_whitespace(__FILE__);
// Newlines are considered whitespace, and are removed too:
do_nothing();
?>

The output:

<?php
 echo php_strip_whitespace(__FILE__); do_nothing(); ?>



==========================================================

PHP微信平台开通啦!
微信搜索“PHPChina”,点击关注按钮,即可获得PPC为您推送的最新最专业的业界信息,更有专题栏目为您献上
【PPC挖掘】:不定时为您献上经典产品与产品人的故事。
【PPC外文】:每日分享一篇外文翻译文章

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/735055.htmlTechArticlePHP有着众多的内置函数,其中大多数函数都被开发者广发使用。但也有一些同样有用却被遗忘在角落,本文将介绍7个鲜为人知功能却非常酷...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn