Home  >  Article  >  Backend Development  >  How to replace the first character in php

How to replace the first character in php

藏色散人
藏色散人Original
2020-07-24 09:24:273199browse

How to replace the first character in php: first create a PHP sample file; then define a "str_replace_limit" method; then implement replacement through if statements and regular matching methods; finally run the file.

How to replace the first character in php

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);
}

$search: the 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: "PHP Tutorial

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