Home > Article > Backend Development > How to use the substr_replace function in php
Usage of substr_replace function in php: [substr_replace(string,replacement,start,length)]. The substr_replace() function is used to replace part of a string with another string.
php The substr_replace function is used to replace a certain string in a string with another string. Its syntax is substr_replace(string,replacement,start,length).
(Recommended tutorial: php video tutorial)
How to use the php substr_replace function?
Function: Replace a certain string in a string with another string.
Syntax:
substr_replace(string,replacement,start,length)
Parameters:
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 replacement at the specified position in the string, negative number - starts replacement at the specified position from the end of the string, 0 - starts replacement at the first character in the string.
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 from the end of the string to be replaced from the end of the substring to be replaced. 0 - Insert instead of replace
Description:
Replace part of a string with another string. If the start argument is negative and length is less than or equal to start, length is 0. This function is binary safe.
php substr_replace() function usage example 1
<?php echo substr_replace("Hello world","php.cn",6); ?>
Output:
Hello php.cn
php substr_replace() function usage example 2
<?php $i = "i like JavaScript"; echo substr_replace($i,"php",7); ?>
Output:
i like php
The above is the detailed content of How to use the substr_replace function in php. For more information, please follow other related articles on the PHP Chinese website!