Advanced string processing technology

Source: Internet
Author: User

String processing

A string is a collection of zero or multiple characters, including numbers, letters, special characters, and invisible characters.

1. Escape and restore the string
The character string is often interpreted as a control character. To solve this problem, you need to escape special characters. Use the addslashes () and stripslashes () functions in php to convert and restore strings.

Addslashes (string), which can be used to add a diagonal line to a string to escape characters in a specified string, including single double quotation marks, backslash, and null characters. It is commonly used to generate SQL statements. Similar to escape, use the stripslashes () function.

These two functions can escape and restore the strings in the specified range. String addcslashes (string $ str, string $ charlist) charlist specifies which strings are prefixed with the backslash \.

 

2. truncate

Truncates a string by using the sunstr () function. Truncates 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, use a negative number as the starting point of the substring, to a substring from the end of the original string. The number of characters equals the absolute value of the given negative number.

String substr (string str, int $ start [, int $ length]) parameters are: string, the starting position of the truncation, and the number of digits (the number of characters to be truncated)

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

3. Split and synthesize strings
 
Splitting refers to storing the content in a string by category according to a specific rule to achieve more functions. The explode () function is used to split a string according to the specified rules. The returned 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 a string, they are separated by the string separator as the boundary points. If the limit parameter is set, the returned array contains up to limit elements, and the last element contains the rest of the string.
Explode (specify the delimiter, separator string [number of elements contained])

Synthesize a string and use the implode () function. The syntax is the same. Combine the elements in the array into a new string. Implode (delimiter, merged data );


4. Replace the string

String replacement technology, which can block illegal characters in posts or message boards, and trace the query keywords in red. The str_ireplace () and substr_replace () functions are used to replace strings.

The srt_ireplace () function replaces the string specified in the original string with the new substring (substring.
Mixed str_ireplace (mixed $ search, mixed $ replace, mixed $ subject [, int & $ count]) replaces search with the replace parameter for all parameters that appear in the subject parameter. The & count parameter indicates the number of times the string is executed.

$ Search specifies the string to be searched. To be replaced
$ Replace: replace the specified value
$ Subject specifies where to replace the search range
Count: an optional parameter to obtain the number of replicas executed.
 

This function is case insensitive and must be case sensitive using srt_replace ()

Substr_replace () function to replace some strings in a specified string .. Parameter: Specifies the string to be replaced with, the starting position of the string to be replaced, and the length to be specified.

 

5. Search for a string, which is applied to string search functions, such as strstr () and substr_count,

The strstr () function returns the substring that appears at the first occurrence of a specified string to the end of the string. If the row is successful, the remaining string is returned; otherwise, false is returned. This function is case sensitive. If it is case insensitive, you can use the stristr () function. (Location where the first image appears)
The last occurrence position. Use the strrchr () function, which is case sensitive. Strripos () is the final position of the query, but is case insensitive.

Substr_count () indicates the number of times a string appears in a string.

6. Remove spaces at the beginning and end of the string and special characters.
The trim () function removes the white spaces and special characters on both sides of the string. lrtim () is the white space and special characters on the left, 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 that specifies the characters to be deleted from the specified string. If this parameter is not set, all optional characters will be deleted,


7. String and html Conversion
The conversion between string and html directly outputs the source code on the webpage without being executed. The htmlentities () function is used (),
Htmlentities (string $ string [, int $ quote_style], [string $ charset]) $ string required parameter: Specifies the string to be converted. quote_Style is an optional parameter. How to process quotation marks in a string, an optional charest parameter to determine the character set for conversion.


8. Regular Expression: a regular expression is a syntax rule that describes the string structure.
 
 
A complete regular expression consists of two parts: metacharacters and text characters. metacharacters are characters with special meanings, and text characters are common texts. PCRE-style regular expressions are generally placed in the separator/middle.

Two regular expression function libraries are provided in php, but the execution efficiency of the PCRE function library is slightly higher than that of the POSIX function library. Therefore, the PCRE function library is used here.

The preg_match () function searches and matches the specified string based on the regular expression mode.
Preg_match_all () Same as above

The preg_grep () function matches the elements in the array.

String replacement
The preg_replace () and preg_replace_callback () functions complete complex string replacement operations.
The preg_replace () function searches and replaces strings based on the regular expression mode.

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

 


Author "Technology is King"

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.