PHP str_ireplace() function
Mga halimbawa
Palitan ang character "WORLD" (walang paghihigit sa pagkakapareho ng maiikling titik) ng string "Hello world!" na may "Shanghai":
<?php echo str_ireplace("WORLD","Shanghai","Hello world!"); ?>
Paglilinang at paggamit
Ang str_ireplace() function ay papalit ang ilang character ng string (walang paghihigit sa pagkakapareho ng maiikling titik).
Ang function na ito ay dapat sundin ang mga sumusunod na alituntunin:
- Kung ang hinahanap na string ay isang array, ito ay ibabalik ang isang array.
- Kung ang hinahanap na string ay isang array, ito ay maghanap at palitan ang bawat elemento ng array.
- Kung kinakailangang hanapin at palitan ang array, at ang bilang ng mga bagay na ipalit ay mas mababa kaysa sa bilang ng nahahanap na bagay, ang mga sobrang bagay ay ipalit ng walang laman na string.
- Kung ito ay tungkol sa paghahanap ng isang array, ngunit pagpalit ng isang string lamang, ang pagpalit na string ay gagamitin sa lahat ng nahahanap na halaga.
Note:This function is not case-sensitive. Please use str_replace() Function to perform case-sensitive search.
Note:This function is binary safe.
Syntax
str_ireplace(find,replace,string,count)
Parameters | Description |
---|---|
find | Required. Specifies the value to be searched for. |
replace | Required. Specifies the replacement find The value of the value of |
string | Required. Specifies the string to be searched. |
count | Optional. A variable to count the replacement number. |
Technical Details
Return Value: | Returns a string or array with replacement values. |
PHP Version: | 5+ |
Update Log: | In PHP 5.0, a new count Parameter. |
More Examples
Example 1
Use with arrays and count The str_ireplace() function of variables:
<?php $arr = array("blue","red","green","yellow"); print_r(str_ireplace("RED","pink",$arr,$i)); // This function is case-insensitive echo "Replacement count: $i"; ?>
Example 2
Use the str_ireplace() function with fewer elements to be replaced than the elements found:
<?php $find = array("HELLO","WORLD"); $replace = array("B"); $arr = array("Hello","world","!"); print_r(str_ireplace($find,$replace,$arr)); ?>