Home > Backend Development > PHP Tutorial > How to use the substr_replace function in php

How to use the substr_replace function in php

藏色散人
Release: 2023-04-06 19:28:02
Original
4311 people have browsed it

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.

How to use the substr_replace function in php

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)
Copy after login

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);
?>
Copy after login

Output:

Hello php.cn
Copy after login

php substr_replace() function usage example 2

<?php
$i = "i like JavaScript";
echo substr_replace($i,"php",7);
?>
Copy after login

Output:

i like php
Copy after login

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!

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