php中str_replace跟str_ireplace的用法和区别
php中str_replace和str_ireplace的用法和区别
str_replace() 函数定义:使用一个字符串替换字符串中的另一些字符,对大小写敏感的搜索语法:str_replace(find,replace,string,count)str_ireplace() 函数定义:使用一个字符串替换字符串中的另一些字符,对大小写不敏感的搜索语法:str_ireplace(find,replace,string,count)
str_replace详解
在都不使用数组时,该函数直接使用replace替换所有的search并返回替换后的字符串。如:str_replace("m","n","my name is jim!")返回ny nane is jin!
1、只对search使用数组。
示例:str_replace(array('m','i'),'n',"my name is jim!");返回:ny nane ns jnn!
可以看出,函数顺序性的对数组中每个字符串进行替换,并返回替换后的字符串。
2、只对replace使用数组。
示例:str_replace('m',array('n','z'),"my name is jim!n")返回:Arrayy naArraye is jiArray!
该替换比较有意思,如果只对第二个参数使用数组则函数将其作为字符串Array进行使用,将所有的search替换为了数组。
3、只对subject使用数组。
示例:str_replace("m","n",array("my name is jim!","the game is over!"))该语句执行结果返回一个数组,即分别为传入的两个字符串替换后的结果。
如果输出数组内容会看到:ny nane is jin! the gane is over!
4、对search和replace都使用数组。
示例:str_replace(array("m","i"),array("n","z"),"my name is jim!")返回:ny nane zs jzn!
查看执行结果可以发现,如果前两个参数都使用数组则函数把数组各个对象项字符串进行了替换,及search的第一项替换为replace的第一项。以此类推。
如果search数组比new_deedle长,例如:str_replace(array("m","i","s"),array("n","z"), "my name is jim!");返回:ny nane z jzn!可见,对于search数组多出来的字符串被替换为了空串。
如果replace数组比search长,例如:str_replace(array("m","i"),array("n","z","x"), "my name is jim!")返回ny nane zs jzn!可见replace多余的项被忽略。
5、三个参数都使用数组。
例如:str_replace(array("m","i"),array("n","z"),array("my name is jim!","the game is over"))返回的数组内容:ny nane zs jzn!the gane zs over
这个比较好理解,对两个字符串分别执行替换。
例子
<pre code_snippet_id="585804" snippet_file_name="blog_20150121_2_4043813" name="code" class="php"><?php $find = array("Hello","world");$replace = array("B");$arr = array("Hello","world","!");print_r(str_ireplace($find,$replace,$arr));?>
输出:
<pre code_snippet_id="585804" snippet_file_name="blog_20150121_3_5354747" name="code" class="php">Array([0] => B[1] =>[2] => !)

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

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

MySQL is a commonly used relational database management system that provides a variety of functions to process and operate data. Among them, the REPLACE function is used to replace the specified part of the string. In this article, we will introduce how to use the REPLACE function for string replacement in MySQL and demonstrate its usage through code examples. First, let’s take a look at the syntax of the REPLACE function: REPLACE(str,search_str,replace_str).

What are the string search and replace techniques in Python? (Specific code example) In Python, strings are a common data type, and we often encounter string search and replace operations in daily programming. This article will introduce some common string search and replacement techniques, accompanied by specific code examples. To find a specific substring in a string, you can use the find() method or index() method of the string. The find() method returns the index of the first occurrence of the substring in the string.

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values of the same keys, making the program design more flexible. array_merge

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

According to news on January 31, TheBrowserCompany recently released a new application called ArcSearch, which makes full use of AI technology and aims to help users obtain the information they need more quickly and conveniently. The core feature of the ArcSearch application is its "Browseforme" function, which is powered by models from companies such as OpenAI. When a user searches, this function can automatically read at least six related web pages, integrate and summarize this information through AI technology, and finally display it to the user on a newly designed page. This page not only contains information related to the search keywords, but also divides the content into different parts to make it clearer for users.

1The basic unit of Unicode computer storage is the byte, which is composed of 8 bits. Since English only consists of 26 letters plus a number of symbols, English characters can be stored directly in bytes. But other languages (such as Chinese, Japanese, Korean, etc.) have to use multiple bytes for encoding due to the large number of characters. With the spread of computer technology, non-Latin character encoding technology continues to develop, but there are still two major limitations: no multi-language support: the encoding scheme of one language cannot be used in another language and there is no unified standard: for example There are many encoding standards in Chinese such as GBK, GB2312, GB18030, etc. Since the encoding methods are not unified, developers need to convert back and forth between different encodings, and many errors will inevitably occur.
