转: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 开发工 ...
随机推荐
- HDFS Architecture--官方文档
HDFS Architecture Introduction The Hadoop Distributed File System (HDFS) is a distributed file syste ...
- vim 学习相关记录
VIM 相关内容****************** vim 的三个模式: 编辑模式 --> 输入模式 --> 末行模式 编辑模式: 通常键入键盘值被理解成一个操作; 如: dd(删除行) ...
- TCP/IP协议原理与应用笔记08:对等层和对等实体
1. 我们直接通过下面这个图,就可以直观了解: Outlook :收发邮件的软件组件. IE:浏览器. CutFTP:文件传输工具. 小结:因为为了完成不同的功能,所以会出现不同实体,这些不同的实体为 ...
- php 链式操作的实现 学习记录
php 面向对象中实现链式操作的关键部分:调用的方法中返回当前对象 ,从而实现链式操作: <?php namespace commom; class db { public function w ...
- 在/etc/password用户名前面加hello,ID前加is
方法2: #!/bin/sh #set -x file=/etc/passwd while read LINE #for i in `cat $file` do #username=`echo $i| ...
- codevs1690开关灯
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #def ...
- 【开源java游戏框架libgdx专题】-12-开发工具-图片合成
TexturePackerGui工具: 1.工具使用: 首先看到texturepacker的界面 界面介绍: New pack:创建项目按钮,单击后输入文件名称,创建文件. Input directo ...
- Android Service生命周期及用法
Service概念及用途:Android中的服务,它与Activity不同,它是不能与用户交互的,不能自己启动的,运行在后台的程序,如果我们退出应用时,Service进程并没有结束,它仍然在后台运行, ...
- WinForm窗体之间传值
当程序需要将一个窗体中的一些信息传给另一个窗体并让其使用时,就需要用到这个知识点 方法一:通过接受参数的窗体的构造函数传值 例:现有Form1和Form2两个窗体,二者都包含一个文本框,Form1还包 ...
- 使用top工具,找出消耗CPU 较多的进程
1.使用top工具,找出消耗CPU 较多的进程 [oracle@cuug ~]$ top top - 10:48:27 up 23:15, 4 users, load average: 1.09, ...