Substr_replace() can be used in php to replace the characters after the first character. Just set the third parameter of the function to 1, which stipulates that the replacement starts from the second character. The syntax "substr_replace(string ,"replacement value",1,number of characters)"; if the number of characters is omitted, all subsequent characters will be replaced.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In php, you can use substr_replace ()Replace the characters after the first character.
substr_replace() function replaces part of a string with another string; it will replace the specified number of characters starting from the specified position.
substr_replace(string,replacement,start,length)
Example: Replace several characters after the first character
<?php $str = 'hello,world,hello,world'; $replace = '-'; echo substr_replace($str, $replace, 1,1)."<br>"; echo substr_replace($str, $replace, 1,2)."<br>"; echo substr_replace($str, $replace, 1,3)."<br>"; echo substr_replace($str, $replace, 1,4)."<br>"; echo substr_replace($str, $replace, 1,5)."<br>"; ?>
If the number of characters length
parameter is omitted, All characters after the first character will be replaced.
<?php $str = 'hello,world,hello,world'; $replace = '-'; echo substr_replace($str, $replace, 1)."<br>"; ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to replace the characters after the first character in a php string. For more information, please follow other related articles on the PHP Chinese website!