str_replace() function in PHP

王林
Release: 2023-09-01 18:30:01
forward
1006 people have browsed it

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)
Copy after login

Parameters

  • find - The value to search for

  • < strong>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;
?>
Copy after login

Output

I am David
Copy after login

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";
?>
Copy after login

Output

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

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template