Please login to start chat

ICT Solutions 2019-02-01 13:09:33
Category : php tutorials

Search and replace in php


<?php
$offset = 0;

if(isset($_POST['text']) && isset($_POST['search']) && isset($_POST['replace'])){
 $text = $_POST['text'];
 $search = $_POST['search'];
 $replace = $_POST['replace'];

 $search_length = strlen($search);
 
 if(!empty($text) && !empty($search) && !empty($replace)){
   while($strpos = strpos($text, $search, $offset)){
   $offset = $strpos + $search_length;
   $text = substr_replace($text, $replace, $strpos, $search_length);
   echo $text;
   }
 }else{
   echo 'Please fill all fields!';
 }
}
?>

<hr />
<form action="searchandreplace.php" method="POST">
<textarea name="text" rows="15" cols="60"></textarea><br /><br />
Search : </br>
<input type="text" name="search"><br /><br />
Replace : </br>
<input type="text" name="replace"><br /><br />
<input type="submit" value="Submit">
</form>

Comments:

No comment to show.

Available Services
Download Android Application