Home  >  Article  >  Backend Development  >  How to replace the first character in php

How to replace the first character in php

青灯夜游
青灯夜游Original
2021-03-10 17:52:212073browse

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.

How to replace the first character in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php replace the first character of the string

<?php
$str1 = &#39;hello world!&#39;;
$str2 = substr_replace($str1,&#39;H&#39;,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)

How to replace the first character in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn