Home >Backend Development >PHP Problem >How to replace the first character in php
In PHP, you can use the substr_replace() function to replace the first character of a string. The syntax format is "substr_replace (specify string object, characters used for replacement, 0, 1)". The substr_replace() function can replace part of a string with another string.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php replace the first character of the string
<?php $str1 = 'hello world!'; $str2 = substr_replace($str1,'H',0,1); print_r($str2); ?>
Output:
Hello world!
[Recommended learning: "PHP Video Tutorial"]
Related function description:
substr_replace() function replaces part of a string with another string. Syntax:
substr_replace(string,replacement,start,length)
Note: If the start parameter is a negative number and length is less than or equal to start, then length is 0.
Return value: Returns the replaced string. If string is an array, the array is returned.
For more programming related knowledge, please visit: Programming Video! !
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!