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

How to replace the first character in php

藏色散人
Release: 2023-03-03 07:54:02
Original
3248 people have browsed it

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

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

Related labels:
php
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