Home  >  Article  >  Backend Development  >  Advanced String Processing Technology_PHP Tutorial

Advanced String Processing Technology_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:46:06832browse

String processing

String, the so-called string is a set of zero or more characters, including: numbers, letters, special characters and invisible characters.

1. Escape and restore string
Strings are often interpreted as control characters. To solve this problem, special characters need to be escaped. In PHP, strings are escaped and restored through the addslashes() and stripslashes() functions.

addslashes (string), you can add slashes to the string to escape the characters in the specified string. The characters that can be converted include single and double quotation marks, backslashes, and null characters. It is commonly used when generating sql statements. Reduction is the same as escaping, using the stripslashes() function.

These two functions can escape and restore strings within the specified range. string addcslashes ( string $str , string $charlist ) charlist specifies which strings are preceded by a backslash.

2. Interception

Intercept the sunstr() function for strings. Extract a certain length of characters from a string at a specified position. If you use a positive number as the starting point of the substring to call this function, you will get the string that ends from the starting point. If you use a negative number as the starting point of the substring, you will get a substring from the end of the original string. The number of characters is equal to the absolute value of the given negative number.

string substr (string str,int $start [,int $length]) The parameters are: string, interception starting position, interception digits (number of intercepted characters)

The strlen() function gets the length of a string.

3. Split and synthesize string

Segmentation refers to classifying and storing the content in a string according to a certain rule to achieve more functions. String splitting uses the explode() function to split a string according to the specified rules, and the return value is an array.
array explode ( string $separator, string $string [, int $limit] ) This function returns an array composed of strings. Each element is a substring of string. They are separated by string separator as boundary points. If the limit parameter is set, the returned array contains up to limit elements, and the last element will contain the remainder of the string.
explode(specify delimiter, separated string [number of elements contained])

To synthesize a string, use the implode() function. The syntax is the same. Combines the elements in an array into a new string. implode(separator, merged data);


4. Replace string

String replacement technology can block illegal characters in posts or message boards, and can highlight query keywords. Use the functions str_ireplace() and substr_replace() to realize the string replacement function.

The srt_ireplace() function replaces the string specified to be replaced in the original string with a new substring (substring).
mixed str_ireplace (mixed $search, mixed $replace, mixed $subject [, int &$count] ) Replaces all search parameters appearing in the parameter subject with the parameter replace. The parameter &count represents the number of times to replace the string.

$search specifies the string to be searched. To be replaced
$replace specifies the replacement value and replaces it with
$subject Specifies the search range Where to replace
count Optional parameter, obtains the number of replacements performed

This function is not case-sensitive. To make it case-sensitive, use srt_replace()

The substr_replace() function replaces part of the string in the specified string. . Parameters: specify which string, what character to replace it with, the starting position of the replacement string, specify the length

5. Retrieve strings, functions applied to string search, like strstr() function and substr_count() function,

The strstr() function obtains the substring from the first occurrence of a specified string in another string to the end of the latter. If the row is successful, the remaining string is returned, otherwise false is returned. This function is case-sensitive. To avoid case-sensitivity, use the stristr() function. (The position where it first appeared)
For the last occurrence, use the strrchr() function, which is case-sensitive. strripos() also checks the last occurrence position, but it is not case sensitive.

substr_count() retrieves the number of times a string appears in a character.

6. Remove leading and trailing spaces and special characters
from the string The trim() function removes the blank characters and special characters on the left and right sides of the string, lrtim() is the blank characters and special characters on the left side, and rtrim() is the right
string ltrim (string $str[,string $charlist]) str is the string object to be operated on. charlist is an optional parameter, specifying which characters need to be deleted from the specified string. If this parameter is not set, all optional characters will be deleted,


7. String and html conversion
To convert between string and html, the source code is directly output in the web page without being executed. Use the function htmlentities(),
htmlentities (string $string [, int $quote_style],[string $charset]) $string required parameter, specify the string to be converted, quote_Style optional parameter, choose how to handle quotes in the string, charest optional parameter, OK The converted character set.


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


A complete regular expression consists of two parts, namely metacharacters and text characters. Metacharacters are characters with special meanings, and text characters are ordinary text. PCRE-style regular expressions are generally 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 terms of execution efficiency, so the PCRE function library is mentioned here.

The preg_match() function searches and matches the specified string according to the regular expression pattern.
preg_match_all() Same as above

The preg_grep() function matches elements in the array

String replacement
The preg_replace() function and preg_replace_callback() function complete complex string replacement operations.
The preg_replace() function completes string search and replacement operations based on regular expression patterns.

String splitting
The preg_split() function completes complex string splitting operations.


Author “Technology is King”

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478615.htmlTechArticleString processing string, the so-called string is a set of zero or more characters, including : Numbers, letters, special characters and invisible characters. 1. Escape and restore characters...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn