PHP | str_replace() Function

PHP str_replace() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP str_replace() Function.

The PHP str_replace() function is used to replace the string with another string.

This function follows some specified rules while working, these rules are given below:

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

This built-in function is the case-sensitive version of str_ireplace() function.

syntax

Following below is the syntax to use this function -

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )


Parameter Details

Sr.NoParameters & Description
1

find

It specifies the string to find

2

replace

It specifies the replace the value in find

3

string

It specifies the string to be searched

4

count

It used to count the number of replaced variable


Return Value

This function returns the string or array with replaced value.

Example

Try out the example below -

<?php
   echo str_replace("html","coders","php coding");
?>

Output

When the above code is executed, it will produce the following result -

php coding


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we will be studying about the PHP str_rot13() Function.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.