Home >Backend Development >PHP Problem >How to replace the first one in php preg
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.
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!