[PHP] How to Replace String in PHP

[PHP] How to Replace String in PHP

[PHP] How to Replace String in PHP - Hello developers, in this article will explained how to replace string in PHP. In PHP Programming, there is a special function to do it. This function is str_replace.

str_replace() function is has 3 parameters, it is str_replace(find, replace, object). For explample, look at below:


1
2
3
4
5
<?php
$object = My name is Aji;
$after_replace = str_replace("Aji", "Paper 4Share", $object);
echo $after_replace;
?>

and the output is
 My name is Paper 4Share

The Explanation About str_replace() PHP Functions

str_replace() function is the function that needed for replace a string or a text. Using this function we are able to replace anyting the words in a long text. Based on the example script above, we are able to replacing a words that contains "Aji".

How this function works?

str_replace() function have a three parameters. All the parameters must be filled in correctly for getting a good result. The parameters of str_replace() functions is str_replace(find, replace, string). Below is the explanation about the paramaters:

  • find, is the parameters to find a search string.
  • replace, is the parameters that used to replacing a string, if the string that searched is found.
  • string, this parameter is used for base of the string that will searched
Okay, its an explanation about str_replace() function in PHP Programming. The parameters must be filled sequentially, because you will get an error message and the function is not working.

In the next article, we will explain about preg_replace().

You need read this:



Paper 4Share - [PHP] How to Replace String in PHP