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.
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.
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);
}
?>

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

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

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

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

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

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

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
