How to use str replace in PHP

In this article I will explain how the str_replace() function can be used in PHP.
  • 1570

str_replace() function in PHP

  • The str_replace() function is used to replace some characters with some other characters in a string.
  • The str_replace() function is case-sensitive.
  • The str_replace() function is binary-safe.

This function works by the following rules:

  • It returns an array, If the string to be searched is an array.
  • Find and replace is performed with every array element, If the string to be searched is an array.
  • If both find and replace are arrays, and replace has fewer elements than find, an empty string will be used as replace
  • The replace string will be used for every find value, If find is an array and replace is a string.

Syntax

str_replace(find,replace,string,count)

Parameter

  • find find is required parameter. it is specify for find the value.
  • replace replace is required parameter. it is specify for value to replace.
  • string string is required parameter. it is specify for search the string.
  • count count is optional parameter. it is variable that counts the number of replacements .
Example

The following example show to how the str_replace() function can be used in PHP.

<html>

<body>

<h3 style="color: blue;">str_replace() function example in PHP</h3>

    <?php

    $arr_list = array("Nitin".'<br/>',"Ravi".'<br/>',"Sachin".'<br/>',"Sunny".'<br/>');

    print_r(str_replace("Ravi","Priyanka",$arr_list,$i));

    echo "<br/>Replacements: $i";

    ?>

</body>

</html>

 

Output

str-replace-php.gif
You may also want to read these related articles here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.