search
HomeBackend DevelopmentPHP Tutorialsubstr_replace() function in PHP
substr_replace() function in PHPAug 27, 2023 pm 07:37 PM
String replacementsubstr_replace()substr() in php

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. If there is any infringement, please contact admin@php.cn delete
php怎么替换回车键php怎么替换回车键Mar 23, 2023 am 11:12 AM

​在PHP程序开发当中,有时需要对文本内容进行处理,其中处理中包括替换回车键。下面我们将简要介绍如何在PHP中实现替换回车键。

如何用php正则替换以什么开头的字符串如何用php正则替换以什么开头的字符串Mar 24, 2023 pm 02:57 PM

PHP正则表达式是一种针对文本处理和转换的有力工具。它可以通过解析文本内容,并按照特定的模式进行替换或截取,达到有效管理文本信息的目的。其中,正则表达式的一个常见应用是替换以特定字符开头的字符串,对此,我们进行如下的讲解

php怎么从左边替换字符串php怎么从左边替换字符串Mar 23, 2023 pm 04:53 PM

PHP是一个广泛使用的脚本语言,可以用来开发各种Web应用和网站。在这些应用中,字符串是一个不可或缺的部分。在很多情况下,我们需要对字符串进行替换、拆分或截取等操作。本文将介绍如何在PHP中从左边替换字符串。

如何在PHP中使用正则表达式替换字符串中的特殊字符如何在PHP中使用正则表达式替换字符串中的特殊字符Jun 24, 2023 am 10:46 AM

随着互联网的不断发展,PHP的应用场景越来越广泛。在PHP的开发中,有时需要替换字符串中的特殊字符,这时可以使用正则表达式进行替换。本文将介绍如何使用正则表达式在PHP中替换字符串中的特殊字符。首先,了解一下正则表达式的基础知识。正则表达式是一种语言,用于描述一些字符串的模式。正则表达式包括一些特殊字符,例如.、*、+、?等,这些特殊字符有特殊的含义。在PH

php如何替换冒号后面的11位数字php如何替换冒号后面的11位数字Mar 21, 2023 pm 04:32 PM

替换字符串是PHP中最基本的操作之一。替换功能在数据处理和字符串操作中至关重要,能够能够提高程序执行效率和性能。在PHP中替换字符串的函数有很多,如:substr_replace,str_replace,preg_replace等等。但是对于特定的字符串替换,可能需要使用特殊的替换规则。在本文中,我们将学习如何替换冒号后面的11位数字。

使用PHP的substr_replace()函数替换字符串中的多个文本使用PHP的substr_replace()函数替换字符串中的多个文本Nov 03, 2023 pm 05:24 PM

PHP是一种广泛应用于网站开发的脚本语言,具有强大的字符串处理功能。在字符串处理过程中,有时需要替换字符串中的多个文本,这时可以使用PHP的substr_replace()函数来实现。substr_replace()函数用于在字符串中替换指定的子字符串,并返回替换后的字符串。它的语法如下:stringsubstr_replace(string$str

php怎么替换中文汉字php怎么替换中文汉字Mar 23, 2023 pm 02:47 PM

PHP作为一种常用的编程语言,具有许多强大的特性。它也提供了丰富的函数库,不仅能够处理常见的数值和字符串,还支持处理多种文件类型,包括中文字符编码。在处理中文字符串的过程中,PHP提供了一些有用的函数来实现中文汉字的替换。

php替换空字符串失败怎么解决php替换空字符串失败怎么解决Mar 23, 2023 pm 03:51 PM

在PHP开发中,字符串替换是非常常见的操作。然而,有时候在进行字符串替换时,可能会遇到替换空字符串的情况,而结果却不尽如人意。本文将探讨为什么PHP替换空字符串会失败,并提供解决方案。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!