转:7个鲜为人知却超实用的PHP函数
PHP have lots of built-in functions, and most developers know many of them. But a few functions are not very well known, but are super useful. In this article, I have compiled little known but really cool PHP functions.
highlight_string()
When displaying PHP code on a website, the highlight_string() function can be really helpful: It outputs or returns a syntax highlighted version of the given PHP code using the colors defined in the built-in syntax highlighter for PHP.
Usage:
<?php
highlight_string('<?php phpinfo(); ?>');
?>
Documentation: http://php.net/manual/en/function.highlight-string.php
str_word_count()
This handy function takes a string as a parameter and return the count of words, as shown in the example below.
Usage:
?php
$str = "How many words do I have?";
echo str_word_count($str); //Outputs 5
?>
Documentation: http://php.net/manual/en/function.str-word-count.php
levenshtein()
Ever find the need to determine how different (or similar) two words are? Then levenshtein() is just the function you need. This function can be super useful to track user submitted typos.
Usage:
<?php
$str1 = "carrot";
$str2 = "carrrott";
echo levenshtein($str1, $str2); //Outputs 2
?>
Documentation: http://php.net/manual/en/function.levenshtein.php
get_defined_vars()
Here is a handy function when debugging: It returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.
Usage:
print_r(get_defined_vars());
Documentation: http://php.net/manual/en/function.get-defined-vars.php
escapeshellcmd()
escapeshellcmd() escapes any characters in a string that might be used to trick a shell command into executing arbitrary commands. This function should be used to make sure that any data coming from user input is escaped before this data is passed to the exec() or system() functions, or to the backtick operator.
Usage:
<?php
$command = './configure '.$_POST['configure_options']; $escaped_command = escapeshellcmd($command); system($escaped_command);
?>
Documentation: http://php.net/manual/en/function.escapeshellcmd.php
checkdate()
Checks the validity of the date formed by the arguments. A date is considered valid if each parameter is properly defined. Pretty useful to test is a date submitted by an user is valid.
Usage:
<?php
var_dump(checkdate(12, 31, 2000));
var_dump(checkdate(2, 29, 2001)); //Output
//bool(true)
//bool(false)
?>
Documentation: http://php.net/checkdate
php_strip_whitespace()
Returns the PHP source code in filename with PHP comments and whitespace removed. This is similar to using php -w from the commandline.
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(); ?>
Documentation: http://www.php.net/manual/en/function.php-strip-whitespace.php
转:7个鲜为人知却超实用的PHP函数的更多相关文章
- 7个鲜为人知却超实用的PHP函数
PHP有许多内置函数,其中大多数函数都被程序员广泛使用.但也有一些函数隐藏在角落,本文将向大家介绍7个鲜为人知,但用处非常大的函数. 没用过的程序员不妨过来看看. 1.highlight_string ...
- 7个鲜为人知却超实用的PHP函数--转(柒捌玖零)
PHP有许多内置函数,其中大多数函数都被程序员广泛使用.但也有一些函数隐藏在角落,本文将向大家介绍7个鲜为人知,但用处非常大的函数. 没用过的程序员不妨过来看看. 1.highlight_string ...
- C++编程技术之 异常处理(上)
增强错误恢复能力是提高代码健壮性的最有力途径之一 之所以平时编写代码的时候不愿意去写错误处理,主要是由于这项工作及其无聊并可能导致代码膨胀,导致的结果就是本来就比较复杂的程序变得更加复杂.当然了,前面 ...
- 我们为什么要看《超实用的Node.JS代码段》
不知道自己Node.JS水平如何?看这张图 如果一半以上的你都不会,必须看这本书,一线工程师用代码和功能页面来告诉你每一个技巧点. 都会一点,但不知道如何检验自己,看看本书提供的面试题: 1. ...
- 关于编程一些鲜为人知的真相 csdn
<关于编程一些鲜为人知的真相>一文讲了一些编程的真相:如果把所有项目的生命周期平均一下,那么一个程序员大概10-20%的时间用来写代码,并且大多数程序员可能每天大约只有10-12行代码会进 ...
- Hover.css:一组超实用的 CSS3 悬停效果和动画
Hover.css 是一套基于 CSS3 的鼠标悬停效果和动画,这些可以非常轻松的被应用到按钮.LOGO 以及图片等元素.所有这些效果都是只需要单一的标签,必要的时候使用 before 和 after ...
- 优秀工具推荐:超实用的 CSS 库,样板和框架
当启动一个新的项目,使用 CSS 框架或样板,可以帮助您节省大量的时间.在这篇文章中,我编译整理了我最喜欢的 CSS 样板,框架和库,帮助你在建立网站或应用程序时更加高效. 您可能感兴趣的相关文章 精 ...
- 一些鲜为人知却非常实用的数据结构 - Haippy
原文:http://www.udpwork.com/item/9932.html 作为程序猿(媛),你必须熟知一些常见的数据结构,比如栈.队列.字符串.链表.二叉树.哈希,但是除了这些常见的数据结构以 ...
- 值得拥有!精心推荐几款超实用的 CSS 开发工具
当你开发一个网站或 Web 应用程序的时候,有合适的工具,绝对可以帮助您节省大量的时间.在这篇文章中,我为大家收集了超有用的 CSS 开发工具. 对于 Web 开发人员来说,找到有用的 CSS 开发工 ...
随机推荐
- 理解Android的startservice和bindservice(转)
一.首先,让我们确认下什么是service? service就是android系统中的服务,它有这么几个特点:它无法与用户直接进行交互.它必须由用户或者其他程序显式的启动.它的优先级比较高,它比处于前 ...
- MVC 文件上传
项目需要,做一个图片上传的功能,本来是很简单,但是需要同时上传多个文件,并分条带一些额外的信息,听上去很复杂,通过下面图就可以一目了然: 网上找过一些方法,但多为不支持图片与其他信息关联,或者分两次上 ...
- Android RecyclerView 瀑布流
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...
- android蓝牙4.0(BLE)开发之ibeacon初步
一个april beacon里携带的信息如下 ? 1 <code class=" hljs ">0201061AFF4C0002159069BDB88C11416BAC ...
- Shell 脚本模拟 milter 实现黑白名单及关键词过滤
程序执行流程:1. 开始接受邮件.2. 检查发件人是否在黑名单内,如果是拒绝接受;否则继续3. 检查发件人是否在白名单内,如果是接收邮件;否则继续4. 对邮件进行关键字过滤,如果邮件中包含被过滤的关键 ...
- HUD 2444 The Accomodation of Students (二分图染色+最大匹配)
#include<iostream> #include<cstdio> #include<cstring> #define maxn 2010 using name ...
- 什么是mimeType?
因特网多媒体邮件扩展标示内容是什么格式.告诉浏览器或者server如何解析该数据http的请求和相应都含有一个mimeType字段 =>content-type(通用首部)
- Android Studio中关于Project与Module
在Android Studio中一个Project和Eclipse中的WorkSpace是相似的,而一个Module与Eclipse中的Project是相似的(大致可以这么的认为) 若在Android ...
- DHCP服务器
DHCP指的是由服务器控制一段IP地址范围,客户机登录服务器时就可以自动获得服务器分配的IP地址和子网掩码.首先,DHCP服务器必须是一台安装有Windows 2000 Server/Advanced ...
- iOS移动端架构的那些事!(转载)
一个app的初始阶段,必然是先满足各种业务需求.然后,经过多次版本迭代之后,先前的由于急于满足需求而导致的杂乱代码则会充斥整个项目.而此时,项目有了一定的规模,有了一定数量的开发人员,那么为了达到快速 ...