7 little-known but very useful PHP functions

Source: Internet
Author: User
Tags bool php code php source code

This article mainly introduces 7 little-known but very practical PHP functions, which are summarized in this article, such as highlight_string (), Str_word_count (), Levenshtein (), Get_defined_vars (), Escapeshellcmd () and other very useful but not common functions, the need for friends can refer to the following

Overview

PHP has a number of built-in functions, most of which are used by developers. But there are some equally useful but forgotten in the corner, this article will introduce 7 little-known features but very cool functions.

Highlight_string ()

The highlight_string () function is useful when you need to display PHP code in a Web page. This function returns the highlighted version of the code in the function through the color defined in PHP.

The code is as follows:

  

Highlight_string (" );

echo highlight_string (' , true ');

?>

Str_word_count ()

This function makes it easy to return the number of words in the input string argument.

The code is as follows:

  

$str = "Hello World";

echo Str_word_count ($STR); Output 2

?>

Levenshtein ()

The function can easily return the Levenshtein (edit distance) between two parameters. Once encountered a need, users in the editing of identity cards, restricting users can only modify 4 digits, using this function.

The code is as follows:

  

$idcard = ' 230406198506206797 ';

$newIdcard = ' 230406198506207798 ';

Echo Levenshtein ($idcard, $newIdcard); Output 2

?>

Get_defined_vars ()

This function is useful when debugging a program, and it returns an array of all the defined variables, including the environment, system, and user-defined variables.

The code is as follows:

  

Var_dump (Get_defined_vars ());

?>

Escapeshellcmd ()

This function is used to skip the special symbols in the string to prevent malicious users from playing tricks on the server system. Can be used with the Exec () and system () functions.

The code is as follows:

  

$command = './configure '. $_post[' configure_options '];

$escaped _command = Escapeshellcmd ($command);

System ($escaped _command);

?>

Checkdate ()

This function can be used to detect the validity of date parameters. It verifies the legality of each parameter entered.

The code is as follows:

  

Var_dump (Checkdate (12, 31, 2000));

Var_dump (Checkdate (2, 29, 2001));

Output

BOOL (TRUE)

BOOL (FALSE)

?>

Php_strip_whitespace ()

This function will return the PHP source code after the deletion of the annotation and space. This is useful for comparing the actual number of code with the number of comments.

The code is as follows:

  

Note 1

/*

* NOTE 2

*/

echo Php_strip_whitespace (__file__);

Do_nothing ();

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.