Php built-in function instance tutorial

Source: Internet
Author: User
Tags function examples glob
Php built-in function examples

Strtolower ()
Strtoupper ()
Ucfirst ()
Ucword ()

Text html tag processing function


Nl2br ()
Htmllentities ()
Htmlspecialchars ()
Stripslashes ()
Strip_tags ()
Number_format ()
Strrev ()
Md5 ()


// Convert to lowercase
$ A = 'www .jb51.net ';
Echo strtolower ($ );
// Result: www.jb51.net

// Convert to uppercase
$ A = 'www .jb51.net ';
Echo strtoupper ($ );
// Result: WWW.jb51.net

// Uppercase letters
$ A = 'www .jb51.net ';
Echo ucfirst ($ );
// Result: Www.jb51.net

// Uppercase letters of each word
$ A = 'I love you ';
Echo ucword ($ );
// Result: I Love You

/*
Tip: We all know the case sensitivity. do you think the case sensitivity is different from the case sensitivity and the case sensitivity?
In Windows, php is not case sensitive, but cannot be case insensitive in linux.
For example
When the class is automatically loaded
Function _ autoload ($ className ){
Include strtolower ($ className). '. class. php ';
}
$ Obj = new MyClass;
In this way, myclass. class. php will be loaded.
Because the file name is often lowercase, it must be converted to lowercase.
?>
*/

// Nl2br converts spaces into entities
Because the line feed shown in the browser is usually
For example, in the form message book, it must be converted. Otherwise, if the line cannot be broken, more spaces will be processed as a space.
$ A ='
I
Love
You
';
Echo $;
Echo nl2br ($ );
Result 1: I love you.
Result 2:
I
Love
You

// If you do not perform html tag processing for form submission, the style or js code will be displayed and run directly.
/*

When you enter
Www.jb51.net
A large font is displayed upon submission.
But you wanted
Result of www.jb51.net
So proceed
When you enter script alert ('www .jb51.net') script
Javascript will be run once submitted
If this is not the case, you have to deal with it to prevent hackers from finding the entrance to your attack.
The default form submission method is get.
*/
// When you enter

Www.jb51.net


Echo htmlspecialchars ($ _ GET ['title']); // filtered
Result:

Www.jb51.net


When you view the source code, you will know that <and> have been replaced with <>. the page prototype is displayed.
If you do not want to handle this

Some copied articles have their own tag styles, which will disrupt your page layout. css conflicts may occur.
Htmllentities () function user and htmlspecialchars () are not used in the opposite way.

The strip_tags () function can be used when you need to keep the tag.
Echo strip_tags ($ _ GET ['title'],'

');
The submitted result is displayed when you view the source code.

No

/*
Add the input I love 'jb51 ';
The submission result is I love \ 'jb51 \ '. the backslash is escaped.
What should I do if I want to output the original text?
You can use this php function stripslashes ()
Cancel escape
Echo stripslashes ($ _ GET ['title']);
The result is I love 'jb51 ';
What if html tags are contained?
I love 'jb51'
What should I do with prototype output? I can combine two functions to use what I have already said.
Echo htmlspecialchars (stripslashes ($ _ GET ['title']);
Result: I love 'jb51'
*/

// Number_format () this function is used to format the currency function. different countries have different habits, so the display of the required currency is different. for example, the Chinese money in the mall is usually in this format.
Use commas (,) to retain the number of decimal places'
This function is easy to use.
Number_format ($ money, which indicates the number of decimal points reserved. 'What are the decimal points separated 'and 'What are the decimal points separated ')
$ Price = '1970. 100 ';
Echo number_format ($ money, 2 ,',','.');
Result: 123.465.789, 23
Echo number_format ($ money, 2, '.', ','); // Chinese
Result: 123,465,789.23

// Strrev () returns the string
$ Str = 'http: // www.jb51.net ';
Echo strrev ($ str );
Result: moc. tenwii. www //: ptth

// Md5 indicates that the user name and password must be encrypted to prevent hackers.
$ A = 'admin ';
Echo $ B = md5 ($ );

Built-in functions are functions provided by PHP. these functions can implement many functions.

Is_int, is_integer, and is_long are used to determine whether the variable is an integer.

Is_float, is_double, and is_real are used to determine whether the variable is a floating point type.

Is_bool determines whether the variable is Boolean.

Is_string determines whether the variable is a string

Is_array

Is_resource: determines whether the variable is a resource type.

Is_object: determines whether a variable is an object.

Is_null determines whether the variable is null

$ _ SERVER ['script _ name'] returns/mantis/test. php, relative path;
_ FILE _ absolute path of the returned file d: \ Projects \ mantis \ test. php
$ _ SERVER ['http _ X_FORWARDED_PROTO ']
$ _ SERVER ['https']
$ _ SERVER ['server _ port']
$ _ SERVER ["REQUEST_URI"] URI is used to specify the page to be accessed.
$ GLOBALS: A Global combination array containing all the variables. The variable name is the key of the array.
Explode splits another string using one string to return a string array.
Implode uses a character to link an array element to a string.
$ _ SERVER ['server _ name'] host NAME of the SERVER where the current script is located
$ _ SERVER ['http _ host'] domain name
$ _ SERVER ['server _ ADDR '] IP address of the SERVER where the current script is located
Trim removes spaces at the beginning and end of a string.
Basename
Dirname returned Directory
Krsort? Sort arrays in reverse order by key name
Set_time_limit () sets the maximum script execution time.

Time () returns the current unix timestamp.

Is_string () determines whether the variable is a string.

Is_bool

Is_Inteter

Is_float

Strtotime -- parses the date and time descriptions of any English text into UNIX timestamps

File_exists check that the file or directory does not exist

Fopen () open the specified file or URL

Feof () determines whether the file pointer is at the end

Fgets () reads a row from the object pointer

Trim () removes spaces before and after a row

Strlen () returns the length of the string.

Str_replace
Isset
Str_replace ("\ 0", '', $ t_self) replace \ 0 in t_self with""
Strpos? Search for the position where the string appears for the first time
$ _ SERVER ['server _ soft'] SERVER information
Realpath returns the normalized absolute path name.
DIRECTORY_SEPARATOR
In linux, PATH_SEPARATOR is a ":" number, and a ";" number on Windows.
Set_include_path: set incldue_path

Get_include_path: get the current include_path

Count () returns the length of the array. if the value is not an array, 1 is returned.

Substr (string, int start [int length]) returns the string of the length starting from the specified position of start.
$ _ SERVER ['Local _ ADDR '] IIS7 is used to obtain the server ip address
$ _ SERVER ['remote _ ADDR '] IP address of the browser user
Array_pad? Fill the array with a value to the specified length
Memory_get_usage () returns the amount of memory allocated to PHP.
Spl_autoload_register registers the _ autoload () function
Ob_get_contents () Return the contents of the output buffer
Strtolower () converts uppercase letters to lowercase letters
Extension_loaded ('zlib ') to check whether the extension is loaded.
Ini_get returns the configuration item value in php. ini.
Ini_set: set the value of the configuration item in php. ini, ini_set ("memory_limit", '256m'); takes effect only during script execution, and script execution completes resetting the value
PHP_EOL PHP line terminator

Preg_replace: replace a regular expression.

Preg_match (string pattern, string subject [, array matches [, int flags]) -- Regular Expression Matching

Htmlspecialchars Convert special characters to HTML entities
Ceil () returns an integer
Floor () Rounding
Round () rounding method

Very useful 10 uncommon PHP built-in functions

1. sys_getloadavg ()

Sys_getloadavt () can obtain the system load. This function returns an array containing three elements, each representing the average load of the system in the past 1, 5, and 15 minutes.

Instead of letting the server go down due to excessive load, it is better to take the initiative to die a script when the system load is very high. sys_getloadavg () is used to help you implement this function. But unfortunately, this function is invalid in windows.


2. pack ()

Pack () can convert a 32-bit hexadecimal string returned by md5 () to a 16-bit binary string, saving storage space.


3. cal_days_in_month ()

Cal_days_in_month () returns the total number of days in a specified month.


4 ._()

WordPress developers often see this function as well as _ e (). These two functions have the same functions and can be used in combination with the gettext () function to implement multilingual website operations. For more information, see the relevant section of the PHP Manual.


5. get_browser ()

Before sending the page, check what your browser can do? Get_browser () can obtain the browser type and functions supported by the browser. However, you need a php_browscap.ini file to provide a reference file for the function.

Note that this function is based on the general features of the browser. For example, if the user closes the browser's support for JavaScript, the function cannot know this. However, this function is very accurate in determining the browser type and OS platform.

6. debug_print_backtrace ()

This is a function used for debugging and can help you find logical errors in the code. To understand this function, let's look at an example:

OUTPUT:
#0 recur () called at [C: \ htdocs \ php_stuff \ index. php: 8]
#1 iterate () called at [C: \ htdocs \ php_stuff \ index. php: 25]
#0 recur () called at [C: \ htdocs \ php_stuff \ index. php: 8]
#1 iterate () called at [C: \ htdocs \ php_stuff \ index. php: 21]
#2 recur () called at [C: \ htdocs \ php_stuff \ index. php: 8]
#3 iterate () called at [C: \ htdocs \ php_stuff \ index. php: 25]
#0 recur () called at [C: \ htdocs \ php_stuff \ index. php: 8]
#1 iterate () called at [C: \ htdocs \ php_stuff \ index. php: 21]
#2 recur () called at [C: \ htdocs \ php_stuff \ index. php: 8]
#3 iterate () called at [C: \ htdocs \ php_stuff \ index. php: 21]
#4 recur () called at [C: \ htdocs \ php_stuff \ index. php: 8]
#5 iterate () called at [C: \ htdocs \ php_stuff \ index. php: 25]

7. metaphone ()

This function returns the metaphone value of a word. words with the same pronunciation have the same metaphone value. that is to say, this function can help you determine whether the two words have the same pronunciation. However, it is invalid for Chinese characters.

8. natsort ()

Natsort () can sort an array by natural sorting. let's look at an example:

Outputs:
# Array
#(
# [0] = & gt; 100 apples
# [1] = & gt; 110 apples
# [2] => 5 apples
# [3] => 55 apples
#)
Natsort ($ items );
Print_r ($ items );
# Outputs:
# Array
#(
# [2] => 5 apples
# [3] => 55 apples
# [0] = & gt; 100 apples
# [1] = & gt; 110 apples
#)

9. levenshtein ()

Levenshtein () tells you the "distance" between two words ". It tells you how many letters need to be inserted, replaced, and deleted if you want to change one word to another. Let's look at an example:

10. glob ()

Glob () makes you think it is silly to use opendir (), readdir () and closedir () to find files.

For more details, see Baidu Encyclopedia http://wenku.baidu.com/view/02c23a2e7375a417866f8ffd.html.

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.