search
HomeBackend DevelopmentPHP TutorialThe function substr_replace() that replaces part of a php string with another string

Example

Replace "Hello" with "world":

<?php
echo substr_replace("Hello","world",0);
?>

Definition and usage

substr_replace() function replaces the string Replace part of it with another string.

Note: If the start parameter is negative and length is less than or equal to start, length is 0.

Note: This function is binary safe.

Syntax

substr_replace(string,replacement,start,length)
Parameters Description
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 at the specified position in the string

  • Negative number - starts at the specified position from the end of the string

  • 0 - Starts 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 to be replaced from the end of the string

  • 0 - Insert instead of replace

Technical details

As of PHP 4.3.3, all parameters accept arrays.

更多实例

实例 1

从字符串的第 6 个位置开始替换(把 "world" 替换成 "earth"):

<?php
echo substr_replace("Hello world","earth",6);
?>

实例 2

从字符串结尾的第 5 个位置开始替换(把 "world" 替换成 "earth"):

<?php
echo substr_replace("Hello world","earth",-5);
?>

实例 3

在 "world" 开头插入 "Hello":

<?php
echo substr_replace("world","Hello ",0,0);
?>

实例 4

一次性替换多个字符串。把每个字符串中的 "AAA" 替换成 "BBB":

<?php
$replace = array("1: AAA","2: AAA","3: AAA");
echo implode("<br>",substr_replace($replace,&#39;BBB&#39;,3,3));
?>

例子:

<?php
echo substr_replace(&#39;abcdef&#39;, &#39;###&#39;, 1);	//输出 a###
echo substr_replace(&#39;abcdef&#39;, &#39;###&#39;, 1, 2);	//输出 a###def
echo substr_replace(&#39;abcdef&#39;, &#39;###&#39;, -3, 2);	//输出 abc###f
echo substr_replace(&#39;abcdef&#39;, &#39;###&#39;, 1, -2);	//输出 a###ef 
?>


Return value: Return the replaced string. If string is an array, the array is returned.
PHP Version: 4+
##Update Log :

The above is the detailed content of The function substr_replace() that replaces part of a php string with another string. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

What is autoloading in PHP?What is autoloading in PHP?Apr 30, 2025 pm 03:37 PM

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

What are PHP streams?What are PHP streams?Apr 30, 2025 pm 03:36 PM

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

What is the maximum size of a file that can be uploaded using PHP ?What is the maximum size of a file that can be uploaded using PHP ?Apr 30, 2025 pm 03:35 PM

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

What is Nullable types in PHP ?What is Nullable types in PHP ?Apr 30, 2025 pm 03:34 PM

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

What is the difference between the unset() and unlink() functions ?What is the difference between the unset() and unlink() functions ?Apr 30, 2025 pm 03:33 PM

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool