Home  >  Article  >  Backend Development  >  str_replace() function in PHP

str_replace() function in PHP

王林
王林forward
2023-09-01 18:29:041000browse

str_replace() function in PHP

str_replace() function is used to replace one string with another string.

Note - This function is case sensitive.

Syntax
str_replace(find, replace, str, count)

Parameters

  • find - The value to search for

  • replace - The string we want to replace $find with

  • str - The string we want to search for

  • count - Number of replacements

Return

str_replace() function returns a string or array with replacement values.

Example

The following is an example-

Real-time demonstration

<?php
$str = "I am Amit";
$res = str_replace("Amit", "David", $str);
echo $res;
?>

Output

I am David

Example

The following is an example -

Live demonstration

<?php
$myArr = array("one","two","three");
print_r(str_replace("three","four",$myArr,$c));
echo "<br>" . "Number of Replacements = $c";
?>

Output

Array (
   [0] => one
   [1] => two
   [2] => four
)
<br> Number of Replacements = 1

The above is the detailed content of str_replace() function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete