Home > Backend Development > PHP Tutorial > php str_ireplace() str_replace usage and differences_PHP tutorial

php str_ireplace() str_replace usage and differences_PHP tutorial

WBOY
Release: 2016-07-20 10:59:11
Original
1623 people have browsed it

In php, str_ireplace() str_replace function is character replacement. I think the biggest difference between them is that one is not case-sensitive and the other is case-sensitive. The others are the same. ​

str_replace() function

Definition: Use a string to replace other characters in a string, case-sensitive search
Syntax:

str_replace(find,replace,string,count)
Example:

The code is as follows Copy code
 代码如下 复制代码

The code is as follows Copy code

Output

hupengstudying.com hupeng

Through the above description and example comparison, I believe everyone has a clear understanding of the difference between the str_replace() function and the str_ireplace() function in PHP
str_replace uses

You can actually run the following code and debug it.


$bodytag = str_replace("%body%", "black", "
// Provides: Hll Wrld f PHP

$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");

$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");

// Provides: You should eat pizza, beer, and ice cream every day
$phrase = "You should eat fruits, vegetables, and fiber every day.";

$healthy = array("fruits", "vegetables", "fiber");

$yummy = array("pizza", "beer", "ice cream");

$newphrase = str_replace($healthy, $yummy, $phrase);

// Use of the count parameter is available as of PHP 5.0.0
$str = str_replace("ll", "", "good golly miss molly!", $count);

echo $count; // 2
 代码如下 复制代码
?> ");

Str_replace detailed explanation
When no array is used, this function directly uses replace to replace all searches and returns the replaced string. For example: str_replace("m","n","my name is jim!") returns ny nane is jin!

1. Only use arrays for search.
Example: str_replace(array('m','i'),'n',"my name is jim!"); Return: ny nane ns jnn!
As can be seen, the function sequentially replaces each string in the array and returns the replaced string.

2. Only use arrays for replace.
Example: str_replace('m',array('n','z'),"my name is jim!n") returns: Arrayy naArraye is jiArray!
This replacement is more interesting. If you only use an array for the second parameter, the function will use it as a string Array, replacing all searches with arrays.

3. Only use arrays for subject.

Example: str_replace("m","n",array("my name is jim!","the game is over!")) The execution result of this statement returns an array, that is, the two passed in The result after string replacement.
If you output the array content, you will see: ny nane is jin! the gane is over!

4. Use arrays for both search and replace.

Example: str_replace(array("m","i"),array("n","z"),"my name is jim!") returns: ny nane zs jzn!
Looking at the execution results, you can find that if the first two parameters use arrays, the function replaces the strings of each object item in the array, and replaces the first item of search with the first item of replace. And so on.
If the search array is longer than new_deedle, for example: str_replace(array("m","i","s"),array("n","z"), "my name is jim!"); return: ny nane z jzn! It can be seen that the extra strings in the search array are replaced with empty strings.
If the replace array is longer than search, for example: str_replace(array("m","i"),array("n","z","x"), "my name is jim!") returns ny nane zs jzn !Visible replacement redundant items are ignored.

5. All three parameters use arrays.

For example: the array returned by str_replace(array("m","i"),array("n","z"),array("my name is jim!","the game is over")) Content: ny nane zs jzn!the gane zs over
This is easier to understand. Replace the two strings separately.


Analysis of the causes of garbled characters in str_replace


The garbled code problem is caused by the working method of Chinese character encoding and str_replace, because a Chinese character occupies two bytes, and a full-width space takes up two bytes like a Chinese character, one area code and one bit code. However, the bit code and the area code overlap. Such as the following code:

The code is as follows Copy code
 代码如下 复制代码

/*
xa1xa1  =>  " " (中文全角空格)
xcdxa1  =>  "汀"
xa1xa3  =>  "。"  (中文全角句号)
xcdxa3  =>  "停"
*/

$str = "汀。";
echo str_replace(" ", "", $str);
?>;

/*

xa1xa1 => " " (Chinese full-width space)

xcdxa1 => "Ting"
xa1xa3 => "." (Chinese full-width period) xcdxa3 => "Stop"

*/

echo str_replace(" ", "", $str); ?>;
The output result of the above code is the word "stop". Because str_replace compares byte by byte, an error will occur http://www.bkjia.com/PHPjc/445620.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/445620.htmlTechArticleThe str_ireplace() str_replace function in php is character replacement. I think the biggest difference between them is the pairing of upper and lower case. Insensitive, one is case sensitive and the others are the same. str_rep...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template