PHP str_replace() string function



The str_replace() function in PHP

The str_replace() function used to replace one string with some another string.

str_replace() function syntax

str_replace(“search for”, “replace”, “original string”);

PHP str_replace() function example

<?php
echo "Replaced Hello = ". str_replace("Hello", "Academy","Hello Meera Academy");
?>

The str_replace() function output is

Replaced string = Academy Meera Academy

In above example the word “hello” replaced by “academy” so out put new string is “Academy Meera Academy”.

php example of str_replace string function.

<html>
<head>
<title>PHP str_replace() Function</title>
</head>
<body>

<FORM method="POST" action="firstexample.php">

Name : <input type="text" name="name"> <br/>

<br/>
<input type="submit" name="Submit1" value="String Replace">

</FORM>

<?php
if(isset($_POST['Submit1']))
{
echo "Replaced Hello = ". str_replace("Hello", "Academy",$_POST["name"]);
}
?>

</body>
</html>

PHP str_replace string function example out put screen like:

PHP str_replace() string fuction
PHP str_replace() string fuction

In above string function example we input “Hello Meera Academy” text in textbox and the word “Hello” replaced by word “Academy” using str_replace php string function.

Leave a Reply

Your email address will not be published. Required fields are marked *