Home > Backend Development > PHP Tutorial > substr_replace() function in PHP

substr_replace() function in PHP

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-08-27 19:38:02
forward
2573 people have browsed it

substr_replace() function in PHP

The substr_replace() function is used to replace part of a string with another string.

Syntax

substr_replace(str,replacement,begin,len)
Copy after login

Parameters

  • str - The string to check

  • < strong>replacement - The string to be inserted

  • begin - The position where the replacement begins -

    • If begin is a positive number: start replacing from the specified position in the string

    • If begin is n number: - start replacing at the specified position from the end of the string

    • If begin is 0 - start replacing from the first character in the string.

  • len - The number of characters to replace. Defaults to the same length as the string.

    • If len is a positive number - the length of the string to be replaced

    • If len is a negative number - how much should be left at the end of the string after replacement characters

    • If len is 0 - insert instead of replace. < /p>

Return

substr_replace() function returns the replaced string.

Example

The following is an example-

Live demonstration

<?php
   echo substr_replace("Demo text","word",5);
?>
Copy after login

Output

The following is the output-

Demo word
Copy after login

Example

Let's see another example, replace string from end of string -

Live Demonstration

<?php
   echo substr_replace("Demo text","word",-5);
?>
Copy after login

Output

Here is the output-

Demoword
Copy after login

The above is the detailed content of substr_replace() function in PHP. For more information, please follow other related articles on the PHP Chinese website!

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