Examples of PHP regular expression matching, replacement and splitting functions

墨辰丷
Release: 2023-03-28 08:14:02
Original
1722 people have browsed it

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]);
Copy after login

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]);
Copy after login

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.

Usage: Replace some more complex content, or convert the content.

Split function

preg_split Regular cutting

preg_split(string pattern,string subject[, int limit[, int flags]]);
Copy after login

Through regular Expression to cut related content, similar to the explode cutting function, but explode can only cut in one way.

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);
?>
Copy after login

Output: (View in source file)

The following is the quotation content:

Array
(
  [0] => Array
    (
      [0] => {title}
      [1] => {content}
    )
  [1] => Array
    (
      [0] => title
      [1] => content
    )
)
Copy after login

The above is the entire content of this article, I hope it will be helpful to Everyone’s learning helps.


Related recommendations:

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!