php之str_replace详解
str_replace
(PHP 4, PHP 5)
str_replace — Replace all occurrences of the search string with the replacement string
Description
mixed str_replace ( mixed$search
, mixed $replace
, mixed $subject
[, int &$count
]
)
This function returns a string or an array with all occurrences of search
in subject
replaced
with the givenreplace
value.
If you don't need fancy replacing rules (like regular expressions), you should always use this function instead ofpreg_replace().
Parameters
If search
and replace
are
arrays, then str_replace() takes a value from each array and uses them to search and replace on subject
.
If replace
has fewer values than search
,
then an empty string is used for the rest of replacement values. If search
is
an array and replace
is a string, then this replacement string is
used for every value of search
. The converse would not make sense,
though.
If search
or replace
are
arrays, their elements are processed first to last.
search
-
The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.
replace
-
The replacement value that replaces found
search
values. An array may be used to designate multiple replacements. subject
-
The string or array being searched and replaced on, otherwise known as the haystack.
If
subject
is an array, then the search and replace is performed with every entry ofsubject
, and the return value is an array as well. count
-
If passed, this will be set to the number of replacements performed.
Return Values
This function returns a string or an array with the replaced values.
Changelog
Version Description 5.0.0 The count
parameter was added.4.3.3 The behaviour of this function changed. In older versions a bug existed when using arrays as both search
andreplace
parameters which caused emptysearch
indexes to be skipped without advancing the internal pointer on thereplace
array. This has been corrected in PHP 4.3.3, any scripts which relied on this bug should remove empty search values prior to calling this function in order to mimic the original behavior.4.0.5 Most parameters can now be an array. Examples
Example #1 Basic str_replace() examples
<?php <br> // Provides: <br> $bodytag = str_replace("%body%", "black", "");<br> <br> // Provides: Hll Wrld f PHP<br> $vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");<br> $onlyconsonants = str_replace($vowels, "", "Hello World of PHP");<br> <br> // Provides: You should eat pizza, beer, and ice cream every day<br> $phrase = "You should eat fruits, vegetables, and fiber every day.";<br> $healthy = array("fruits", "vegetables", "fiber");<br> $yummy = array("pizza", "beer", "ice cream");<br> <br> $newphrase = str_replace($healthy, $yummy, $phrase);<br> <br> // Provides: 2<br> $str = str_replace("ll", "", "good golly miss molly!", $count);<br> echo $count;<br> ?>
Example #2 Examples of potential str_replace() gotchas
<?php <br> // Order of replacement<br> $str = "Line 1\nLine 2\rLine 3\r\nLine 4\n";<br> $order = array("\r\n", "\n", "\r");<br> $replace = '<br>';<br> <br> // Processes \r\n's first so they aren't converted twice.<br> $newstr = str_replace($order, $replace, $str);<br> <br> // Outputs F because A is replaced with B, then B is replaced with C, and so on...<br> // Finally E is replaced with F, because of left to right replacements.<br> $search = array('A', 'B', 'C', 'D', 'E');<br> $replace = array('B', 'C', 'D', 'E', 'F');<br> $subject = 'A';<br> echo str_replace($search, $replace, $subject);<br> <br> // Outputs: apearpearle pear<br> // For the same reason mentioned above<br> $letters = array('a', 'p');<br> $fruit = array('apple', 'pear');<br> $text = 'a p';<br> $output = str_replace($letters, $fruit, $text);<br> echo $output;<br> ?>
Notes
Note: This function is binary-safe.

PHP在现代编程中仍然是一个强大且广泛使用的工具,尤其在web开发领域。1)PHP易用且与数据库集成无缝,是许多开发者的首选。2)它支持动态内容生成和面向对象编程,适合快速创建和维护网站。3)PHP的性能可以通过缓存和优化数据库查询来提升,其广泛的社区和丰富生态系统使其在当今技术栈中仍具重要地位。

在PHP中,弱引用是通过WeakReference类实现的,不会阻止垃圾回收器回收对象。弱引用适用于缓存系统和事件监听器等场景,需注意其不能保证对象存活,且垃圾回收可能延迟。

\_\_invoke方法允许对象像函数一样被调用。1.定义\_\_invoke方法使对象可被调用。2.使用$obj(...)语法时,PHP会执行\_\_invoke方法。3.适用于日志记录和计算器等场景,提高代码灵活性和可读性。

Fibers在PHP8.1中引入,提升了并发处理能力。1)Fibers是一种轻量级的并发模型,类似于协程。2)它们允许开发者手动控制任务的执行流,适合处理I/O密集型任务。3)使用Fibers可以编写更高效、响应性更强的代码。

PHP社区提供了丰富的资源和支持,帮助开发者成长。1)资源包括官方文档、教程、博客和开源项目如Laravel和Symfony。2)支持可以通过StackOverflow、Reddit和Slack频道获得。3)开发动态可以通过关注RFC了解。4)融入社区可以通过积极参与、贡献代码和学习分享来实现。

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP不是在消亡,而是在不断适应和进化。1)PHP从1994年起经历多次版本迭代,适应新技术趋势。2)目前广泛应用于电子商务、内容管理系统等领域。3)PHP8引入JIT编译器等功能,提升性能和现代化。4)使用OPcache和遵循PSR-12标准可优化性能和代码质量。

PHP的未来将通过适应新技术趋势和引入创新特性来实现:1)适应云计算、容器化和微服务架构,支持Docker和Kubernetes;2)引入JIT编译器和枚举类型,提升性能和数据处理效率;3)持续优化性能和推广最佳实践。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

WebStorm Mac版
好用的JavaScript开发工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境