Home > Article > Backend Development > How to replace the first character in php
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.
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!