7 little-known but very useful PHP functions _php Tutorial

Source: Internet
Author: User
Tags php source code

7 little-known but very useful PHP functions


This article mainly introduces 7 little-known but very useful PHP functions, this article summarizes such as highlight_string (), Str_word_count (), Levenshtein (), Get_defined_vars (), Escapeshellcmd () and other very useful but uncommon 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. The function returns the highlighted version of the code in the function, using the built-in color defined in PHP.

The code is as follows:

  

Highlight_string (");

Echo highlight_string (", true);

?>

Str_word_count ()

This function can easily return the number of words in the input string parameter.

The code is as follows:

  

$str = "Hello World";

echo Str_word_count ($STR); Output 2

?>

Levenshtein ()

This function can conveniently return the Levenshtein (edit distance) between two parameters. Once encountered a demand, the user when editing the identity card, the limit user can only modify 4 digits, the use of 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 containing all the defined variables, including the environment, system, and user-defined variables.

The code is as follows:

  

Var_dump (Get_defined_vars ());

?>

Escapeshellcmd ()

The function is used to skip the special symbols in the string to prevent malicious users from playing tricks on the server system. You can use the Exec () with the system () function.

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 a date parameter. It can verify the legitimacy 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 returns the PHP source code after the comment and space has been deleted. 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 ();

?>

http://www.bkjia.com/PHPjc/1025325.html www.bkjia.com true http://www.bkjia.com/PHPjc/1025325.html techarticle 7 little-known but very useful PHP functions this article mainly introduces 7 little-known but very useful PHP functions, this article summarizes such as highlight_string (), Str_word_count (), Levensh ...

  • Related Article

    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.