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

How to replace the first one in php preg

藏色散人
藏色散人Original
2022-01-26 09:45:231660browse

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 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!

Statement:
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