Home > Backend Development > PHP Problem > How to replace the characters after the first character in a php string

How to replace the characters after the first character in a php string

青灯夜游
Release: 2023-03-15 20:52:02
Original
3367 people have browsed it

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.

How to replace the characters after the first character in a php string

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)
Copy after login

Example: Replace several characters after the first character

<?php
$str = &#39;hello,world,hello,world&#39;;
$replace = &#39;-&#39;;
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>";
?>
Copy after login

How to replace the characters after the first character in a php string

If the number of characters length parameter is omitted, All characters after the first character will be replaced.

<?php
$str = &#39;hello,world,hello,world&#39;;
$replace = &#39;-&#39;;
echo substr_replace($str, $replace, 1)."<br>";
?>
Copy after login

How to replace the characters after the first character in a php string

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template