PHP str_replace function

The function of PHP str_replace function is to replace certain specified characters in string with new characters. The str_replace function can also be used to replace the content of the array . After PHP 5.0.0, the str_replace function can count the number of replacements. The usage is very similar to the substr_replace function. The difference is that the str_replace function can directly count the number of replacements, and there is no need to specify where to start the replacement. You can directly replace the content to be replaced with the new content, which is very simple and easy to use. .

PHP str_replace function syntax

str_replace( search, replace, subject, count)

The PHP str_replace function has four parameters. The first parameter search is the string to be searched, which is a required item. The second parameter replace is the string to be used to replace search and is also a required item. The third parameter is subject. It is the original string or array and is also a required item. The fourth parameter count is a new function of PHP 5.0.0, which is a new parameter used to count the number of replacements. This is an optional item. Below we have prepared three The actual operation example of str_replace function is for your reference.

PHP str_replace function example 1. Replace string content

<?php
  echo str_replace("Disney World","Wibibi","Welcome to Disney World.");
?>

Example output

Welcome to Wibibi.

The example one uses the first three parameters of str_replace, replacing Disney World in the string with Wibibi. From the output of the example, it can be seen that the str_replace function can not only replace single words, but also replace characters, such as blanks or For other punctuation marks, this is a fairly common usage of the str_replace function.

PHP str_replace function example 2: adding count statistics

<?php
  echo str_replace("Disney World","Wibibi","Welcome to Disney World.",$i).'<br>';
  echo'Replaced Count:'.$i;
?>

Example output

Welcome to Wibibi.
Replaced Count:1

The second example is quite simple, it just uses the new function of str_replace. The fourth parameter count is very simple to use. Just set a variable $i in the position of the fourth parameter, and finally use echo to directly change the variable $i The output is the number of replacements.

PHP str_replace function example three, replace the array content

<?php
  $TestArray = array("A","B","C","D");
  print_r(str_replace("B","C",$TestArray,$n));
  echo "Replaced Count: $n";
?>

Example output

Array
(
     [0] => A
     [1] => C
     [2] => C
     [3] => D
)
Replaced Count: 1

The third example is different from the previous two examples. The third example uses the str_replace function to replace the contents of the PHP array . Assuming there is an array $TestArray with four array elements, we want to replace the second letter with C. Just change Put $TestArray into the standard position of the str_replace function. The usage of other parameters is the same as the previous two examples. Finally, we output the array processed by str_replace with print_r , and we can find that the array elements have been replaced. Example 3 also counts the total The number of replacements, the value is 1.

Post a Comment

0 Comments