search
HomeBackend DevelopmentPHP Tutorialstr_replace method to replace a string only once_PHP tutorial
str_replace method to replace a string only once_PHP tutorialJul 21, 2016 pm 03:12 PM
phpreplacestrstrtrexiststringusmethodreplaceofKnow

We all know that in PHP, Strtr, strreplace and other functions can be used to replace, but they replace all of them every time. For example:
"abcabbc", if this string is used as above function to replace b among them, then it will replace them all, but what if you want to replace only one or two? See the solution below:
This is a rather interesting question, just before I have also done similar processing. At that time, I directly used preg_replace to implement it.

mixed preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit] )
Search for a match of the pattern pattern in subject and replace it with replacement. If limit is specified, only limit matches are replaced; if limit is omitted or has a value of -1, all matches are replaced.
Because the fourth parameter of preg_replace can limit the number of replacements, it is very convenient to deal with this problem in this way. But after looking at the function comments about str_replace on php.net, we can actually pick out a few representative functions.

str_replace_once
The idea is to first find the location of the keyword to be replaced, and then use the substr_replace function to directly replace it.

Copy code The code is as follows:

function str_replace_once($needle, $replace, $haystack) {

// Looks for the first occurrence of $needle in $haystack

// and replaces it with $replace.

$pos = strpos($haystack, $needle);

if ($pos === false) {

return $haystack;

}

return substr_replace($haystack, $replace, $pos, strlen($needle));

}

?>

str_replace_limit
Still uses preg_replace, but its parameters are more like preg_replace, and some special characters are escaped, making it more versatile.

Copy code The code is as follows:

function str_replace_limit($search, $replace, $subject, $limit=-1) {

// constructing mask(s)...

if (is_array($search)) {

foreach ($search as $k=>$v) {

$search[$k] = ''' . preg_quote($search[$k],''') . ''';

}

}

else {

$search = '`' . preg_quote($search,''') . '`';

}

//replacement

return preg_replace($search, $replace, $subject, $limit);

}

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326666.htmlTechArticleWe all know that in PHP, Strtr, strreplace and other functions can be used for replacement, but they replace each time Sometimes all are replaced, for example: "abcabbc", if this string uses the above...
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
使用java的StringBuilder.replace()函数替换指定范围的字符使用java的StringBuilder.replace()函数替换指定范围的字符Jul 24, 2023 pm 06:12 PM

使用java的StringBuilder.replace()函数替换指定范围的字符在Java中,StringBuilder类提供了replace()方法,可以用来替换字符串中指定范围的字符。该方法的语法如下:publicStringBuilderreplace(intstart,intend,Stringstr)上面的方法用于替换从索引star

5分钟掌握PyCharm替换快捷键,轻松提升编程速度!5分钟掌握PyCharm替换快捷键,轻松提升编程速度!Feb 22, 2024 am 10:57 AM

PyCharm是一款常用的Python集成开发环境,拥有丰富的功能和快捷键,能够帮助开发者提高编程效率。在日常的编程过程中,掌握PyCharm的替换快捷键技巧可以帮助开发者更快捷地完成任务。本文将为大家介绍PyCharm中一些常用的替换快捷键,帮助大家轻松提升编程速度。1.Ctrl+R替换在PyCharm中,可以使用Ctrl+R快捷键来进行替换操

PyCharm新手指南:替换功能全面解析PyCharm新手指南:替换功能全面解析Feb 25, 2024 am 11:15 AM

PyCharm是一款功能强大的Python集成开发环境,具有丰富的功能和工具,能够极大地提高开发效率。其中,替换功能是开发过程中经常用到的功能之一,能够帮助开发者快速修改代码并提高代码质量。本文将详细介绍PyCharm的替换功能,并结合具体的代码示例,帮助新手更好地掌握和使用该功能。替换功能简介PyCharm的替换功能可以帮助开发者在代码中快速替换指定的文本

使用jQuery替换元素的class名称使用jQuery替换元素的class名称Feb 24, 2024 pm 11:03 PM

jQuery是一种经典的JavaScript库,被广泛应用于网页开发中,它简化了在网页上处理事件、操作DOM元素和执行动画等操作。在使用jQuery时,经常会遇到需要替换元素的class名的情况,本文将介绍一些实用的方法,以及具体的代码示例。1.使用removeClass()和addClass()方法jQuery提供了removeClass()方法用于删除

PyCharm替换快捷键,让编程更得心应手!PyCharm替换快捷键,让编程更得心应手!Feb 21, 2024 pm 12:03 PM

PyCharm是一款广受程序员欢迎的集成开发环境,它提供了强大的功能和工具,让编程变得更加高效和便捷。而在PyCharm中,合理设置和替换快捷键是提高编程效率的关键之一。本文将介绍如何在PyCharm中替换快捷键,让编程更加得心应手。一、为什么要替换快捷键在PyCharm中,快捷键可以帮助程序员快速完成各种操作,提高编程效率。然而,每个人习惯不同,有些人可能

MySQL中如何使用REPLACE函数替换字符串中的指定部分MySQL中如何使用REPLACE函数替换字符串中的指定部分Jul 25, 2023 pm 01:18 PM

MySQL是一种常用的关系型数据库管理系统,它提供了多种函数来处理和操作数据。其中,REPLACE函数是用来替换字符串中的指定部分内容的。在本文中,将介绍如何在MySQL中使用REPLACE函数进行字符串替换,并通过代码示例来演示其用法。首先,我们来了解一下REPLACE函数的语法:REPLACE(str,search_str,replace_str)其

Python中的字符串查找和替换技巧有哪些?Python中的字符串查找和替换技巧有哪些?Oct 20, 2023 am 11:42 AM

Python中的字符串查找和替换技巧有哪些?(具体代码示例)在Python中,字符串是一种常见的数据类型,我们在日常编程中经常会遇到字符串的查找和替换操作。本文将介绍一些常用的字符串查找和替换技巧,并配以具体的代码示例。查找子串在字符串中查找特定的子串可以使用字符串的find()方法或者index()方法。find()方法返回子串在字符串中第一次出现的位置索

如何使用Python在Excel中替换一个单词?如何使用Python在Excel中替换一个单词?Sep 16, 2023 pm 10:21 PM

在Python中,我们可以使用一个名为openpyxl的第三方Python库将Excel中的一个单词替换为另一个单词。MicrosoftExcel是一个用于管理和分析数据的有用工具。使用Python,我们可以自动化一些Excel数据管理任务。在本文中,我们将了解如何使用Python在Excel中替换一个单词。安装openpyxl在Excel中替换Word之前,我们需要使用Python包管理器在系统中安装openpyxl库。要安装openpyxl,请在终端或命令提示符中输入以下命令。Pipinst

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
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

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.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)