Home > Article > Backend Development > Examples of PHP regular expression matching, replacement and splitting functions
This article mainly introduces the PHP regular expression matching, replacement and segmentation functions, briefly analyzes the PHP regular expression matching, replacement and segmentation related functions, and demonstrates the related operation skills of PHP regular expression matching in the form of examples. Friends who need it can For reference,
is as follows:
The functions of regular expressions in PHP mainly include: segmentation, matching, search and replacement.
Matching function
preg_match_all All matching functions
##
preg_match_all (string pattern,string subject,array matches[, int flags]);Yes The results are sorted so that $matches[0] is an array of all pattern matches. Purpose: Intercept more precise content, used to collect web pages, analyze text, etc.
Replacement function
preg_replace Regular replacement function
preg_replace(mixed pattern,mixed replacement,mixed subject[, int limit]);Pass Regular expression to replace relevant content. ① 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.
Split function
preg_split Regular cutting
preg_split(string pattern,string subject[, int limit[, int flags]]);Through regular Expression to cut related content, similar to the explode cutting function, but explode can only cut in one way.
Example demonstration
Matching functionThe following is the reference content:<?php $str="标题:{title}内容:{content}"; $mode="/{(.*)}/U"; preg_match_all($mode,$str,$arr); print_r($arr); ?>Output: (View in source file) The following is the quotation content:
Array ( [0] => Array ( [0] => {title} [1] => {content} ) [1] => Array ( [0] => title [1] => content ) )The above is the entire content of this article, I hope it will be helpful to Everyone’s learning helps.
How to use EL expressions in JS to obtain context parameters
How to use elexpression and non-empty judgment in js
PHP regularExpression Example analysis of match replacement and segmentation functions
##
The above is the detailed content of Examples of PHP regular expression matching, replacement and splitting functions. For more information, please follow other related articles on the PHP Chinese website!