PHP str_replace() Function


The str_replace() is function is used to replace some character with some other characters.

Syntax
str_replace(find,replace,string,count)
Parameters
  • find - (Required) Specify the value to found.
  • replace - (Required) Specify the value to replace.
  • string - (optional) Specifies the string to search.
  • count - (optional) A variable that counts the number of replacement.
Example
<?php
   echo str_replace("Morning","Evening","Hello Good Morning!");
?>

Output

Hello Good Evening!
Example
<?php
    $find = array("HELLO","GOOD","MORNING");
    $replace = array("A");
    $arr = array("Hello","GOOD","MORNING","!");
    print_r(str_replace($find,$replace,$arr));
?>

Output

Array ( [0] => A [1] => [2] => [3] => ! )

Prev Next