ArticleDirectory
- Correct use of PHP function preg_split
Correct use of PHP function preg_split
Name: csdn () font size: T | T
The syntax of the PHP function preg_split is array preg_split (string $ pattern, string $ subject [, int $ limit [, int $ flags]). We will give an example to describe the usage of this function in detail.
AD:
For beginners, you do not have a thorough understanding of the PHP function preg_split, because it is complicated and difficult to use. Today we will introduce it to you in detail, hoping to help readers better understand the PHP function preg_split.
- In-depth discussion on how PHP deletes database tables
- In the string replacement operation
- PHP Regular Expression preg_match rules
- Introduction to the definition and syntax of PHP function trim ()
- How to format a string using the PHP function sprintf ()
Description: array preg_split (string $ pattern, string $ subject [, int $ limit [, int $ flags])
Returns an array that contains the substrings separated by subject along the boundary matching pattern.
If the limit parameter is specified, a maximum of limit substrings are returned. If the limit parameter is-1, there is no limit. It can be used to specify the optional parameter flags.
Flags can be any combination of the following tags (using bitwise OR operator | combination ):
Preg_split_no_empty
If this flag is set, preg_split () returns only non-null components.
Preg_split_delim_capture
If this mark is set, the parentheses expression in the delimiter mode will also be captured and returned. This tag is added to PhP 4.0.5.
Preg_split_offset_capture
If this tag is set, the offset of the affiliated string is also returned for each matching result. Note that this changes the value of the returned array, so that each unit is also an array. The first item is the matching string, and the second item is its offset in the subject. This tag is available from PhP 4.3.0.
Prompt
If you do not need the regular expression function, you can choose to use a faster (or simpler) Alternative function, such as explode () or str_split ().
Example 1672. php function preg_split: Obtain the search string component
- // Split the phrase by any number of commas or space characters,
- // Which include "", \ r, \ t, \ n and \ f
- $ KEYWORDS = preg_split ("/[\ s,] +/", "hypertext language, programming ");
- ?>
For example, the 1673. php function preg_split Splits a string into characters.
- $ STR = 'string ';
- $ Chars = preg_split ('//', $ STR,-1, preg_split_no_empty );
- Print_r ($ chars );
- ?>
Example 1674. php function preg_split Splits a string into a match and Its offset
- $ STR = 'string ';
- $ Chars = preg_split ('//', $ STR,-1, preg_split_no_empty );
- Print_r ($ chars );
- ?>
The PHP function preg_split example will be output:
- Array ([0] =>
- Array ([0] => hypertext [1] => 0) [1] =>
- Array ([0] => language [1] => 10) [2] =>
- Array ([0] => programming [1] => 19 ))
[Responsible editor: Cao kai]
Address: http://developer.51cto.com/art/200911/165373.htm