PHP string processing and regular expressions

Source: Internet
Author: User
Tags first string sprintf

String

1. The string in PHP is a basic data type and PHP does not have local support for Unicode.

2. Strings can be accessed by curly braces to each character, and each curly brace can hold only one character:

? ? $str = ' ABCD ';
? ? $str {1} = ' Change it ';
? ? Echo $str; Accd

3. In double quotes the variable interpolation is parsed, and the contents in the double quotation marks are parsed into a string,

? ? If you encounter the $ symbol, you will be greedy to parse as many legal variable names as possible, using curly braces to enclose the variable names;

? ? When parsing an array index or an object property in double quotation marks, you can also enclose it in curly braces;

4. Commonly used string output functions:

? ? Echo (): output string;

? ? Print (): Outputs a live or string that performs less efficiently than Echo, but provides a return value with a success of 1 and a failure of 0;

? ? Die (): Outputs a message and exits the current script, like the die usage in Perl; exit () an alias with tax;

? ? printf (): formatted output, similar to C language;

? ? sprintf (): Saves the formatted string to a variable as a return value, such as a percentage: $f = sprintf ('%2.2f ', $f *100);

5. String formatting functions:

? ??

? ? A bit more, in the future work to use more, where *trim () and Python is Strim () similar, used to remove some of the line blank, such as more useful; NL2BR () feeling may be helpful, probably because it seems to be better in the way of the front end Htmlspecialchars () handle <, >, and other special symbols are very good, do not remember the special expression, the program will be more readable, and stripslashed () and other functions, in the Web page text processing will be more useful, in the future to more summary.

? ? The Number_format () function can pass in four positional parameters, in order, number, reserved decimal places, decimal separator (default = '. '). ), the thousand split symbol (the default is ', '), because it is a positional parameter, so if the fourth parameter is set, then the preceding parameters are required;

? ? MD5 () is to use encryption algorithm to return an encrypted string after line encryption, the encryption of the file can use Md5_file ();

6. String comparison function

? ? strcmp (): Compare two strings, case-sensitive, compare the character Ascall code size in the order of the strings, note that lowercase is greater than uppercase, and Perl compares strings using the same way;

? ? STRCASECMP (): Compares two strings, not case-sensitive;

? ? STRNATCMP (): If there is a number (the beginning of the number appears to be the same), compare the number size, the same number size is compared to the string size:

Regular expressions

PHP supports a regular expression of two sets, a set of Pcre, Perl-compatible regular expressions, preg_-prefixed functions, and a regular expression of the POSIX extension syntax, using ereg_-prefixed functions, PCRE provides a slightly more efficient regular expression.

1. The regular expression syntax is the same as in Perl;

2. String Lookup

? ? Preg_match (): Accepts three parameters, the first is pattern, the second is a specific string, the third parameter provides an array to save the matching content, where 0 is labeled as the whole, and 1 begins to represent the captured content;

? ? Preg_match_all (): PHP does not have a pattern correction symbol: g, with this function substitution, complete the search function, the difference is that the function accepts the fourth optional parameter, when the fourth parameter is Preg_set_order, the value of subscript 0 begins to represent the captured content;

? ? Preg_grep (): The second parameter is no longer a string, but an array containing a string that matches each element in the array, and returns a successful match;

? ? Strstr (): Passes in two strings, searches for the second string in the first string, returns the part from the first string from the first searched position, and stristr () is a case-insensitive version;

? ? Strpos (): Returns the position of the first occurrence of a string in another string, or False,strrpos () is the version to look up from the end if it is not found;

? ? SUBSTR (): Returns a part of a string specifying the start and end positions;

3. String substitution:

? ? Preg_replace (): The first parameter is the pattern, the second parameter is the need to replace the content, the third parameter is a string, the first two parameters can be a single pattern and a string, or an array containing multiple patterns, strings, the contents of the array should correspond to the same length;

? ? Str_replace (): The first parameter is the lookup string, the second argument is the replacement string, the third parameter is the string content to be replaced, the fourth number of times to save the substitution, and str_ireplace () is the case-insensitive version;

4. String segmentation and linking:

? ? Preg_split (): Pass in two parameters, the first parameter passes in the matching pattern, the second is the string that needs to be split, the third optional parameter limits the number of substrings to return, and the fourth parameter flag is any combination of the following tags:

? ? Preg_split_no_empty: Make Preg_split () return only non-empty components;
? ? Preg_split_delim_capture: Makes it possible to capture and return parenthesis expressions;
? ? Preg_split_offset_capture: To return each occurrence of the matching results at the same time, return the offset of the owning string, the final most a two-tuple array;

? ? Explode (): Pass in two strings, the first is a split tag string, the second is a split string, the third optional parameter limits the number of substrings returned, and the equivalent of Split ();

? ? Implode (): The first argument is a link string, the second argument provides an array of strings, and returns the string after the string in the linked array using the link string;

? 5. In the above three basic operations, if the string function can be completed using the string function as far as possible, the regular expression as far as necessary to use the

PHP string processing and regular expressions

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.