Seven little-known but very practical PHP functions

Source: Internet
Author: User
This article mainly introduces seven little-known but very practical PHP functions. This article summarizes such functions as highlight_string (), str_word_count (), levenshtein (), get_defined_vars (), and escapeshellcmd () and other very useful but uncommon functions. For more information, see Overview

PHP has many built-in functions, most of which are widely used by developers. However, there are also some functions that are useful but forgotten in the corner. This article will introduce seven functions that are little-known but cool.

Highlight_string ()

When PHP code needs to be displayed on a webpage, the highlight_string () function is very useful. This function uses the color defined in PHP to return the highlighted version of the code in the function.

The code is as follows:


<? Php
Highlight_string ('<? Php echo "hello world";?> ');
Echo highlight_string ('<? Php echo "hello world";?> ', True );
?>

Str_word_count ()

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

The code is as follows:


<? Php
$ Str = "hello world ";
Echo str_word_count ($ str); // output 2
?>

Levenshtein ()

This function can easily return the levenshtein (edit distance) between two parameters ). You have encountered a requirement that you can only modify four digits when editing your ID card. This function is used.

The code is as follows:


<? Php
$ Idcard = '000000 ';
$ NewIdcard = '000000 ';
Echo levenshtein ($ idcard, $ newIdcard); // output 2
?>

Get_defined_vars ()

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

The code is as follows:


<? Php
Var_dump (get_defined_vars ());
?>

Escapeshellcmd ()
This function is used to skip special characters in a string to prevent malicious users from cracking the server system. It can be used with exec () and system () functions.

The code is as follows:


<? Php
$ Command = './configure'. $ _ POST ['configure _ options'];
$ Escaped_command = escapeshellcmd ($ command );
System ($ escaped_command );
?>

Checkdate ()

This function can be used to check the validity of the date parameter. It verifies the validity of each input parameter.

The code is as follows:


<? Php
Var_dump (checkdate (12, 31,200 0 ));
Var_dump (checkdate (2, 29,200 1 ));
// Output
// Bool (true)
// Bool (false)
?>

Php_strip_whitespace ()

This function returns the PHP source code after the comments and spaces are deleted. This is useful for comparing the actual number of codes and the number of comments.

The code is as follows:


<? Php
// 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.