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 ();
?>