Home > Backend Development > PHP Problem > How to replace the first one in php preg

How to replace the first one in php preg

藏色散人
Release: 2023-03-15 06:18:02
Original
1682 people have browsed it

How to replace the first one with php preg: 1. Create a PHP sample file; 2. Pass "function str_replace_limit($search, $replace, $subject, $limit=-1) {...} ” method can be used to replace it.

How to replace the first one in php preg

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

How to replace the first php preg?

PHP replacement, only replace the first matched

function str_replace_limit($search, $replace, $subject, $limit=-1) {
    if (is_array($search)) {
        foreach ($search as $k=>$v) {
        $search[$k] = '`' . preg_quote($search[$k],'`') . '`';
        }
    }
    else {
        $search = '`' . preg_quote($search,'`') . '`';
    }
return preg_replace($search, $replace, $subject, $limit);
}
Copy after login

Copy code

$search: String or array to be replaced

$replace: The value to be replaced

$subject: The text to be replaced

$limit: The number to be replaced

Recommended learning: "PHP Video Tutorial

The above is the detailed content of How to replace the first one in php preg. 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