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

substr_replace() function in PHP

WBOY
WBOYforward
2023-08-27 19:37:062482browse

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)

Parameters

  • str - The string to check

  • 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.

Return

substr_replace() function returns the replaced string.

Example h2>

The following is an example-

Live demonstration

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

Output

The following is the output-

Demo word

Example

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

Live Demonstration

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

Output

Here is the output-

Demoword

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:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete