Summary of common functions and library functions in PHP

Source: Internet
Author: User
Tags php web development strtok
first introduced the relatively simple but essential and practical knowledge, can be used as a manual query, suitable for the novice like me to see.

Introduction to PHP Common library functions

First, PHP string operation commonly used functions
1. Determine the length of the string
int strlen (String str)
2. Comparison of two strings
A. The strcmp function compares a binary security to two strings and is case-sensitive
int strcmp (string str1,string str2)
B. Comparing two strings in a case-insensitive manner
int strcasecmp (string str1,string str2)

3. Ask for two identical parts of the string
int strspn (string str1,string str2)
4. Ask for two different parts of a string
5.int strcspn (String str1,string str2)
6. Handling String case
A. Converting a string all to lowercase
String Strtolower (String str)
B. Converting a string all to uppercase
String Strtoupper (String str)
C. Capitalize the first character of a string
String Ucfirst (String str)
D. Converting the first character of each word in a string to uppercase
String Ucwords (String str)
7. Convert strings to HTML
A. Converting a newline character to an HTML termination tag
String bl2br (String str)
B. Converting special characters to wieldhtml equivalent form (non-parsing format)
String Htmlentities (String Str[,int quote_style[,int charset])
String Htmlspecialchars (String Str[,int quote_style[,string charset])
C. Converting HTML to plain text, removing all PHP and HTML tags
String Strip_tags (String str[,string allowable_tags])
D. Converting text to HTML equivalent form
Array get_html_translaction_table (int table[,int Quote_style])
E. Creating a custom conversion manifest
String Strtr (String Str,array replacements)
8. Alternative functions for regular expression functions
A. strtok function parses a string based on a predefined list of strings
String Strtok (String str,string tokens): Returns all content until the tokens is encountered
B. Parsing a string based on a predefined delimiter
Array explode (string separator,string str[,int limit]): Split string
C. Converting an array to a string
String implode (string delimiter, array array)
D. Find the first occurrence of a string
int Strpos (string str,string substr[,int offset])
E. Finding the last occurrence of a string
int Strrpos (string Str,char substr[,offset])
F. Replacing all instances of a string with another string
Mixed Str_replace (String occurrence,mixed replacement,mixed str[,int count])
G. Getting a portion of a string strstr returns the remainder of the first occurrence of a predefined string in a string
String Strstr (string str,string occurrence)
H. Returning part of a string based on a predefined offset
String substr (String str,int start[,ing length]): Start can be negative, indicating the beginning of the countdown
I. Determining how often a string appears
int Substr_count (string str,string substring)
J. Replacing a part of a string with another string
String Substr_replace (String str,string replacement,int start[,int length])
9. Populating and rejecting strings
A. Cropping a character starting from a string
String LTrim (String str[,string charliset])
B. Cropping a character from the end of a string
String RTrim (String str[,string charliset])
C. Clipping characters from both ends of a string
String Trim (String str[,string charliset])
D. Populating a string
String Str_pad (String Str,int length[,string pad_string[,int Pad_type])
10. Character and Word Count
A. Character count in a string
Mixed Count_chars (string Str[,mode])
B. Count the total number of words in a string
Mixed Str_word_count (string str[,int format])
Ii. Three forms validation functions commonly used in PHP Web development

(1) isset ();--suitable for detecting the presence of this parameter. To avoid referencing non-existent variables

Definition and scope: used to test whether a variable has a value (including 0,false, or an empty string returns true, but not null), that is: "http://localhost/?fo=" is also detectable and therefore not applicable. However, if the "http://localhost/" parameter does not contain the FO parameter, it can be detected with isset, at which point the isset ($_get[' fo ') returns false

Not applicable: This function is not suitable for validating the text in an HTML form in an efficient way. To check whether the user input text is valid, you can use empty ();

(2) empty ();--a function that is best used to check if a variable has a null value

Definition and scope: used to check if a variable has a null value: include: empty string, 0,null, or false, which return false, that is: "http://localhost/?fo=" or "http://localhost/?fo=0", The result of empty detection is ture

Not applicable: Not available for detection of 0 parameters

(3) Is_numeric ();--Check if the variable is a number

Definition and scope: Check if the variable is a number, only for the test number

Not applicable scope: But if the parameter name does not exist, it will be wrong, so it is not suitable for the first layer detection

Another useful validation function is Checkdate (Month,day, $year), which is used to confirm whether a date exists or whether it existed in the past.

Comprehensive Example:

Here is the form:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

Third, other common library functions

(1) Ini_set ini_get--operable configuration parameter list
In order to make our program more compatible in different platforms, many times we have to get the current PHP operating environment parameters.
For example, we used to:
Gets the MAGIC_QUOTES_GPC state to determine whether we are escaping (addslashes) data when the form is submitted;
Set Max_execution_time to extend the execution time of the program;
Set error_reporting to make your project in the development and operation phase switch;
Set Memory_limit increase memory, etc...
(2) Ini_set (string varname, String newvalue)://Set parameters for the environment configuration
Ini_get (String varname)://Get Parameters for environment configuration
The PHP ini_set function is the value in the SET option, which takes effect after the function is executed, and when the script finishes, this setting also fails. Not all options can be changed by the function set. Specific values can be set, you can view the list in the manual
In fact, you put PHP ini_set function and ini_get combination so that, very good. For example, if you want to add your own file path to the config file, but you have no permission to change php.ini, you can combine two functions:
Ini_set (' include_path ', Ini_get (' include_path '). ':/your_include_dir: ');
(3) ChDir (dirname (FILE)); Switch to the directory where global.php is located
(4) Ob_start (' Ui_handler ');//Set the output buffer handle to Ui_handler, which is what the first page of the system defines for the Ui_handler function
(5) int intval (mixed var, int [base]);
This function converts a variable into an integer type. The parameter base that can be omitted is the base of the conversion, and the default value is 10. The converted variable var can be an array or any type variable other than the class.
(6) error_reporting (report_level) function--Set the error level of PHP and return to the current level
Where Report_level values are 0, 1, 2, 4, 8, 16, 、......、 4096, 8191
Example: Any number of the above options can be connected (with OR or |) using "or" to report all required levels of errors. For example, the following code turns off user-defined errors and warnings, performs certain actions, and then reverts to the original error level:

<?php//Disable Error Reporting error_reporting (0);//Report Run-time error error_reporting (E_error | e_warning | E_parse);//Report All Errors error_reporting (E_all);? >

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.