This article describes the character replacement functions of two php instances for beginners. one is str_ireplace () and the other is substr_replace (). both functions are easy to use. For more information, see.
This article tells php beginners about the character replacement functions of two php instances, str_ireplace () and substr_replace.
String replacement can be implemented using two common functions: str_ireplace () and substr_replace ().
Str_ireplace () function
Replace the string specified in the original string with the new substring. syntax:
Mixed str_ireplace (mixed search, mixed replace, mixed subject [, int & count])
Search: Required parameter. it specifies the string to be searched.
Replace: a required parameter that specifies the value to replace.
The subject parameter is a required parameter that specifies the search range.
The count parameter is an optional parameter (an optional parameter with brackets). This parameter is used to obtain the number of replicas.
The instance code is as follows:
- $ Str2 = "xx ";
- $ Str1 = "**";
- $ Str = "the address of XX website is www.phpfensi.com. xx website records some notes and thoughts about learning php and various software knowledge ";
- Echo str_ireplace ($ str2, $ str1, $ str); // value searched for str2, value replaced by str1, str range
- ?>
In this example, we will demonstrate the str_ireplace () function with array and count variables. the code is as follows:
- $ Arr = array ("blue", "red", "green", "yellow ");
- Print_r (str_ireplace ("red", "pink", $ arr, $ I ));
- Echo "Replacements: $ I ";
- ?>
- // Output:
- Array
- (
- [0] => blue
- [1] => pink
- [2] => green
- [3] => yellow
- )
Replacements: 1 example 3,The code is as follows:
- $ Find = array ("Hello", "world ");
- $ Replace = array ("B ");
- $ Arr = array ("Hello", "world ","! ");
- Print_r (str_ireplace ($ find, $ replace, $ arr ));
- ?>
- // Output:
- Array
- (
- [0] => B
- [1] =>
- [2] =>!
- )
Substr_replace () function
Replaces some strings in a specified string. syntax:
String substr_replace (string str, string repl, int start, [int length])
Parameter str: specifies the original string to be operated.
Parameter repl: a required parameter that specifies the new string after replacement.
Parameter start: specifies the position at which the string is replaced.
Parameter length: specify the length of the returned string.
The instance code is as follows:
- Substr_replace ('egg', 'X',-1,-1); // eggxs
- Substr_replace ('egg', 'X',-1,-2); // eggxs
- Substr_replace ('egg', 'X',-1,-2); // eggxs
- ?> Same:
- Substr_replace ('egg', 'X',-); // eggxs
- ?>
- Substr_replace ('huevos ', 'X',-2,-2); // huevxos
- Substr_replace ('huevos ', 'X',-2,-3); // huevxos
- Substr_replace ('huevos ', 'X',-2,-3); // huevxos
- ?> Same:
- Substr_replace ('huevos ', 'X',-2, 0); // huevxos
- ?>