Home >Backend Development >PHP Problem >How to replace the last character in a string in php
The way PHP replaces the last character in a string is through the preg_replace() function. The syntax of this function is: [preg_replace(mixed $pattern, mixed $replacement, mixed $subject].
To replace the last character in the string, This can be achieved through the preg_replace() function.
(If you want to know more related tutorials, you can visit php Chinese website .)
Function introduction:
preg_replace function performs a regular expression search and replacement.
Syntax:
mixed preg_replace(mixed $pattern, mixed $replacement, mixed $subject[, int $limit = -1[, int &$count]])
means to search for the part of the subject that matches pattern and replace it with replacement.
Parameters:
$pattern: The pattern to search for, which can be a string or an array of strings.
$replacement: used for The string or string array to replace.
$subject: The target string or string array to search for and replace.
$limit : Optional, the maximum number of substitutions for each subject string per pattern. Default is -1 (unlimited).
$count: Optional, for substitutions The number of executions.
Code implementation:
For example, we want to replace '1' with '0':
$image_path = '//m.sbmmt.com/1'; $str = preg_replace('#.$#i', '0', $image_path);//第二个参数要替换的内容
The above is the detailed content of How to replace the last character in a string in php. For more information, please follow other related articles on the PHP Chinese website!