Home > Backend Development > PHP Problem > How to replace the specified string in php

How to replace the specified string in php

青灯夜游
Release: 2023-03-15 16:14:01
Original
2286 people have browsed it

In PHP, you can use the substr_replace() function to replace the specified number of characters in the string. The syntax is "substr_replace(string, replacement value, $start, 1)"; among them, the parameter "$ start" specifies the position where the replacement starts, which can be a negative value.

How to replace the specified string in php

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

php replaces the specified number Several strings

In PHP, you can use the substr_replace() function to replace characters at specified positions in the string.

<?php
$str = &#39;abcdef&#39;;
echo substr_replace($str, "A", 0,1)."<br>";
echo substr_replace($str, "B", 1,1)."<br>";
echo substr_replace($str, "C", 2,1)."<br>";
echo substr_replace($str, "D", 3,1)."<br>";
echo substr_replace($str, "E", 4,1)."<br>";
echo substr_replace($str, "F", 5,1)."<br>";
?>
Copy after login

How to replace the specified string in php

Description:

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

Syntax:

substr_replace($string,$replacement,$start,$length)
Copy after login
ParametersDescription
stringRequired. Specifies the string to check.
replacementRequired. Specifies the string to be inserted.
startRequired. 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

substr_replace() Replaces the substring qualified by the start and optional length parameters in a copy of string string using replacement.

If start is a positive number, replacement will start from the start position of string. If start is negative, the replacement will start at the start position from the bottom of string.

If the length parameter is set and is a positive number, it represents the length of the replaced substring in string. If set to a negative number, it represents the number of characters from the end of the substring to be replaced from the end of string. If this parameter is not provided, the default is strlen(string) (the length of the string). Of course, if length is 0, then the function of this function is to insert replacement at the start position of string.

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

How to replace the specified string in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to replace the specified string in php. 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