String Advanced processing Technology _php Tutorial

Source: Internet
Author: User
String processing

A string, called a string, is a collection of 0 or more characters, consisting of: numbers, letters, special characters, and invisible characters.

1. Escape, restore the string
Strings are often interpreted as controls, and special characters need to be escaped for such a problem. PHP escapes and restores strings through the addslashes () and stripslashes () functions.

Addslashes (String), you can add a slash to a string, escape the characters in the specified string, and include single double quotes, backslashes, and null characters. It is commonly used when generating SQL statements. Restore and escape, using the Stripslashes () function.

These two functions can be escaped and restored to a string of the specified range content. String Addcslashes (String $str, String $charlist) charlist is the string that specifies which strings precede the backslash \.

2. Interception

Intercepts the SUNSTR () function on a string. Intercepts a certain length of character from a string at a specified position. If the function is called using a positive number as the starting point of the substring, the string ending at the beginning of the string, if used as a substring from the beginning of the call, will be a substring from the end of the original string, a character equal to the absolute value of the given negative numbers.

String substr (String Str,int $start [, int $length]) parameters are: strings, intercept start positions, intercept bits (number of characters to intercept)

The strlen () function Gets the length of the string.

3. Splitting, synthesizing strings

Segmentation refers to the classification of the contents of a string according to a rule to store, in order to achieve more functionality. The segmentation of a string uses the explode () function to split a string by the specified rules and returns an array of values.
Array explode (string $separator, string $string [, int $limit]) This function returns an array of strings, each of which is a substring of string separator as a boundary Points to be split out. If the limit parameter is set, the returned array contains a maximum of limit elements, and the last element will contain the remainder of the string.
Explode (Specify delimiter, delimited string [number of elements included])

Synthetic strings, using the implode () function. The same syntax. Combines the elements in an array into a new string. Implode (delimiter, merged data);


4. Replacing strings

String replacement technology, you can block the post or the message version of the illegal characters, you can query the keyword understandriginally, using the function Str_ireplace () and Substr_replace () to implement the string substitution function.

The Srt_ireplace () function replaces the string in the original string that is specified to be replaced with a new substring (substring).
Mixed Str_ireplace (mixed $search, mixed $replace, mixed $subject [, int & $count]) All parameters that appear in parameter subject are set to parameter Repla CE replacement. The parameter &count represents the number of times the substitution string executes.

$SEARCH Specifies the string to find. To be replaced.
$replace The specified replacement value is replaced by the
$SUBJECT specifies where to replace the range of lookups
Count optional parameter, gets the number of execution substitutions

This function is case-insensitive and is case-sensitive using srt_replace ()

The Substr_replace () function replaces the partial string in the specified string. Parameters: Specifies which string, what character to replace, the starting position of the replacement string, the specified length

5. Retrieves a string that is applied to a string lookup function, such as the STRSTR () function and the Substr_count () function,

The STRSTR () function gets a substring that specifies the first occurrence of a string in another string to the end of the latter. If the row succeeds, the remaining string is returned, otherwise false is returned. This function is case-sensitive, or case-sensitive, using the STRISTR () function. (The first occurrence of the position)
The last occurrence of the position, with the STRRCHR () function, which is case-sensitive. Strripos () is also the last occurrence, but not case sensitive.

Substr_count () Retrieves the number of occurrences of a string in a character.

6. Remove string leading and trailing spaces and special characters
The trim () function removes whitespace and special characters from the left and right sides of the string, Lrtim () is the white space character and special character, and RTrim () is
String LTrim (String $str [, String $charlist]) str is the string object to manipulate. Charlist is an optional parameter that specifies which characters need to be removed from the specified string, and if the argument is not set, all the optional characters are deleted.


7. String and HTML conversion
The conversion between the string and the HTML directly outputs the source code in the Web page without being executed, using the function htmlentities (),
Htmlentities (string $string [, int $quote _style],[string $charset]) $string The necessary parameters, specify the string to be converted, Quote_style optional parameters, Select how to handle quotation marks in a string, Charest optional parameters, to determine the character set of the conversion.


8. Regular expression: a regular expression is a syntax rule that describes the structure of a string.


A complete regular expression consists of two parts, a meta-character and a literal character, which is a character with a special meaning, and the literal character is the normal text. Pcre-style regular expressions are usually placed in the delimiter/middle.

PHP provides two sets of regular expression function libraries, but the Pcre function library is slightly better than the POSIX function library in execution efficiency, so here is the Pcre function library.

The Preg_match () function searches and matches the specified string according to the pattern of the regular expression.
Preg_match_all () ibid.

Preg_grep () function to match elements in an array

Substitution of strings
The Preg_replace () function and the Preg_replace_callback () function complete complex string substitution operations.
The Preg_replace () function completes the search and replace operation of the string based on the pattern of the regular expression.

Segmentation of strings
The Preg_split () function completes complex string splitting operations.


The author "technology is king"

http://www.bkjia.com/PHPjc/478615.html www.bkjia.com true http://www.bkjia.com/PHPjc/478615.html techarticle String processing string, the so-called string is a collection of 0 or more characters, consisting of: numbers, letters, special characters and invisible characters. 1. Escape, restore characters ...

  • 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.