The example of this article describes the PHP regular expressionmatching replacement and segmentation functions. Share it with everyone for your reference, the details are as follows:
The functions of regular expressions in PHP mainly include: segmentation, matching, search and replacement.
1. Matching function
preg_match_all All matching function
preg_match_all (string pattern,string subject,array matches[, int flags]);
Sorts the results so that $matches[0] is an array of all pattern matches.
Purpose: Intercept more precise content, used to collect web pages, analyze text, etc.
2. Replacement function
preg_replace Regular replacement function
preg_replace(mixed pattern,mixed replacement,mixed subject[, int limit]);
Replaces related content through regular expressions.
① The replacement content can be a regular expression or an array;
② The replacement content can be solved by using the modifier e to solve the replacement execution content.
Usage: Replace some more complex content, or convert the content.
3. Split function
preg_split Regular cutting
preg_split(string pattern,string subject[, int limit[, int flags]]);
Use regular expressions to cut related content, similar to the explode cutting function, but explode can only Cut one way.
4. Example demonstration
Matching function
The following is the reference content:
<?php $str="标题:{title}内容:{content}"; $mode="/{(.*)}/U"; preg_match_all($mode,$str,$arr); print_r($arr); ?>
Output: (View in the source file )
The following is the quotation:
Array ( [0] => Array ( [0] => {title} [1] => {content} ) [1] => Array ( [0] => title [1] => content ) )
I hope this article will be helpful to everyone in PHP programming.
For more relevant articles on examples of PHP regular expression matching, replacement and segmentation functions, please pay attention to the PHP Chinese website!
Related articles:
Detailed explanation of the most commonly used regular expressions in PHP
php regular expressions filter html tags, spaces, Line break code
php method of regular replacement of characters specified by variables