In PHP, you can use the strtr() function to implement string replacement.
First of all, let’s briefly understand the definition and syntax of the strtr() function.
Syntax:
string strtr( string $str, string $from, string $to)
The first parameter represents the string to be converted. The second parameter represents the source character in the string corresponding to the destination character to to be converted. The third parameter represents the destination character in the string corresponding to the character from to be converted.
Example:
<?php $str = "PHP中文网"; echo strtr($str, 'P', 'A') . '<br>'; echo strtr($str, 'PP', 'z1') . '<br>'; echo strtr($str, 'P', ' ') . '<br>'; echo strtr($str, 'PH', '12') . '<br>'; echo "<hr>"; ?>
The output results are as follows:
##For more related tutorials, please pay attention tophp中文网.
The above is the detailed content of How to replace a certain character in a string in php. For more information, please follow other related articles on the PHP Chinese website!