Home  >  Article  >  Backend Development  >  How to replace the third character of a string in php

How to replace the third character of a string in php

青灯夜游
青灯夜游Original
2022-01-11 18:40:472213browse

In PHP, you can use the substr_replace() function to replace the third character of the specified string. This function can start from the specified position in the string and replace the characters of the specified length; the syntax is "substr_replace(string , "replacement value", 2,1)".

How to replace the third character of a string in php

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

In php, you can use substr_replace () function to replace the third character of the specified string.

Example:

<?php
$str = &#39;12345678&#39;;
$replace = &#39;h&#39;;
echo substr_replace($str, $replace, 2,1);
?>

How to replace the third character of a string in php

Description:

substr_replace() function replaces characters Replace part of a string with another string.

The substr_replace() function can replace characters of the specified length starting from the specified position in the string.

Syntax:

substr_replace(string,replacement,start,length)
Parameters Description
string Required. Specifies the string to check.
replacement Required. Specifies the string to be inserted.
start Required. Specifies where in the string to begin replacement.
  • Positive number - starts at the specified position in the string
  • Negative number - starts at the specified position from the end of the string
  • 0 - at the first character in the string Start at
length Optional. Specifies how many characters to replace. The default is the same as the string length.
  • Positive number - the length of the string to be replaced
  • Negative number - the number of characters to be replaced from the end of the string
  • 0 - insert instead of replace

Return value: Returns the replaced string. If string is an array, the array is returned.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to replace the third character of a string 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