Home >Backend Development >PHP Tutorial >Introduction to the usage of str_replace() character replacement function in php
This article introduces the usage of str_replace() function in PHP and related examples. Friends in need, please refer to it.
Definition and usage The str_replace function uses one string to replace other characters in a string. grammar str_replace(find,replace,string,count) Parameter Description find required. Specifies the value to look for. replace required. Specifies the value to replace the value in find. string required. Specifies the string to be searched for. count optional. A variable counting the number of substitutions. Tips and Notes Note: This function is case sensitive. Please use str_ireplace() to perform a case-insensitive search. Note: This function is binary safe. Let’s take a look at a few examples about the str_replace() function. Only by looking at more examples and practicing more can you improve. Example 1: <?php echo str_replace("world","John","Hello world!"); ?> Output: Hello John! Example 2: <?php /** * 演示带有数组和 count 变量的 str_replace() 函数: * edit bbs.it-home.org */ $arr = array("blue","red","green","yellow"); print_r(str_replace("red","pink",$arr,$i)); echo "Replacements: $i"; ?> Output: Array ( [0] => blue [1] => pink [2] => green [3] => yellow ) Replacements: 1 Example 3: <?php $find = array("Hello","world"); $replace = array("B"); $arr = array("Hello","world","!"); print_r(str_replace($find,$replace,$arr)); ///by bbs.it-home.org ?> Output: Array ( [0] => B [1] => [2] => ! ) |