How to implement search and replace in php regular

藏色散人
Release: 2023-03-06 17:04:01
Original
2798 people have browsed it

In PHP, you can use the regular expression "preg_replace ($pattern, $replacement, $subject, $limit, $count)" to achieve search and replacement.

How to implement search and replace in php regular

Recommended: "PHP Video Tutorial"

php regular search and replacement preg_replace

preg_replace — Perform a regular expression search and replacement

Method description:

preg_replace ( $pattern , $replacement , $subject , $limit , $count)
Copy after login

Search for the part of the subject that matches pattern and replace it with replacement.

$limit, $count parameters are optional

limit: The maximum number of substitutions for each pattern on each subject. The default is -1 (unlimited).

count: If specified, will be filled with the number of completed substitutions.

Return value:

If subject is an array, preg_replace() returns an array, otherwise it returns a string.

If a match is found, the replaced subject is returned, otherwise the unchanged subject is returned. If an error occurs, NULL is returned.

Instance 1:


        
Copy after login

Instance 2:

 '{offset}', 'period' => '{period}', 'date' => '{date}' ); $patterns = array( '/{offset}/', '/{period}/', '/{date}/' ); $replacements = array( 33, 'day', '216-11-11' ); $url = preg_replace($patterns, $replacements, $PIWIK_API); //结果: /* $url = array(3) { ["filter_offset"]=> string(2) "33" ["period"]=> string(3) "day" ["date"]=> string(9) "216-11-11" } */
Copy after login

The above is the detailed content of How to implement search and replace in php regular. 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
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!