PHP替换,只替换匹配到的第一个

时间:2022-01-02 00:48:18
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:要替换的字符串或者数组

$replace:要替换的值

$subject:要替换的文本

$limit:替换的此数字