-3

I'm new to PHP and try to implement some stuff on my own without using all built-in PHP functions. Here I want to simply search and replace stuff in my text as str_replace() does it. Except I try to do it without that function.

Right now I have this code:

<?php

$offset = 0;

parse_str(file_get_contents("php://input"), $_POST);
if (isset($_POST['text'])&&isset($_POST['search'])&&isset($_POST['replace'])) {
     $text = $_POST['text'];
     $search = $_POST['search'];
     $replace = $_POST['replace'];
     $search_len = strlen($search);
     $strpos = strpos($text,$search,$offset);
        while ($strpos<=strlen($text)){
            $offset = $strpos + $search_len;
            $text = substr_replace($text,$replace,$strpos,$search_len);//line 15
            $strpos = strpos($text,$search,$offset);
 }
  echo $text;

 } ?>

<hr>
<form action="new.php" method="POST" name="form">
  <textarea name="text" rows="6" cols="30" ></textarea><br><br>
  search  <br><br>
  <input type="text" name="search" ><br><br>
  replace<br><br>
  <input type="text" name="replace" ><br><br>
  <input type="submit" value="Submit"><br><br>
</form>

But for some reason my while loop falls into an infinite loop and the scripts ends with an error.

Error:

Fatal error: Maximum execution time of 30 seconds exceeded in C:\Users\Piyush\PhpstormProjects\untitled\new.php on line 15

I fail to see why, where and how my while loop falls into an infinite loop?

Rizier123
  • 58,877
  • 16
  • 101
  • 156
Piyush Verma
  • 51
  • 1
  • 7
  • 3
    Why not just: `echo str_replace($search, $replace, $text);`? – Rizier123 Jun 25 '16 at 01:12
  • 1
    what @Rizier123 said + what happens if $search = "" – Brad Kent Jun 25 '16 at 01:15
  • 1
    yes thats right but actually i was new to php and trying to implement different string functions. i cant figure out whats wrong with above code, if we keep the string functions same – Piyush Verma Jun 25 '16 at 01:19
  • @Brad Kent there should be another nested if statement with ! empty conditions i should possibly add – Piyush Verma Jun 25 '16 at 01:22
  • 2
    @PiyushVerma Then you want to look at the manual page of `strpos()`. It will return FALSE when it doesn't find the needle in the haystack anymore. And FALSE in a numerical context will convert to 0. So that is why your loop falls into an infinite one. – Rizier123 Jun 25 '16 at 01:46
  • So you want to check your `$strpos` for FALSE. – Rizier123 Jun 25 '16 at 01:53

1 Answers1

0

Just use str_replace, Don't go against the grain.

http://php.net/manual/en/function.str-replace.php

str_replace — Replace all occurrences of the search string with the replacement string

<?php

parse_str(file_get_contents("php://input"), $_POST);
if (isset($_POST['text'])&&isset($_POST['search'])&&isset($_POST['replace'])) {
     $text = $_POST['text'];
     $search = $_POST['search'];
     $replace = $_POST['replace'];
     $text = str_replace($search, $replace, $text);
     echo $text;

} 
?>

<hr>
<form action="new.php" method="POST" name="form">
  <textarea name="text" rows="6" cols="30" ></textarea><br><br>
  search  <br><br>
  <input type="text" name="search" ><br><br>
  replace<br><br>
  <input type="text" name="replace" ><br><br>
  <input type="submit" value="Submit"><br><br>
</form>
edhurtig
  • 2,331
  • 1
  • 25
  • 26
  • 1
    Not sure why you downvoted... it is pretty clear that the problem is with your custom find/replace while loop. And it makes no sense to use such a manual and buggy solution when there is an optimized API alternative just sitting there for you to use. – edhurtig Jun 25 '16 at 01:55
  • 1
    OP mentions in the comments that he does not want to use `str_replace()`. See: http://stackoverflow.com/questions/38024020/infinite-while-loop-while-replacing-stuff-in-text#comment63489227_38024020 – Rizier123 Jun 25 '16 at 02:20
  • Alright... understood – edhurtig Jun 25 '16 at 03:19
  • thanks for help, really appreciate it – Piyush Verma Jun 25 '16 at 06:01