title: string replace without using any in-built function
body: <p>With a program to replace substring in a string without using str_replace </p>
  
  <p>should be generic function should work for below example: </p>
  
  <ol>
  <li><p>Word :         Hello world
  Replace word : llo
  Replace by   : zz
  Output should be: Hezz world</p></li>
  <li><p>Word :         Hello world
  Replace word : o
  Replace by   : xx
  Output should be: Hellxx wxxrld </p></li>
  </ol>
  
  <p>This is what i wrote to solve it</p>
  
  <pre><code>function stringreplace($str, $stringtoreplace, $stringreplaceby){
      $i=0;$add='';
      while($str[$i] != ''){
          $add .= $str[$i];
          $j=0;$m=$i;$l=$i;$check=0;
          if($str[$i] == $stringtoreplace[$j]){
              while($stringtoreplace[$j] != ''){
                  if($str[$m] == $stringtoreplace[$j]){
                      $check++;
                  }
                  $j++;$m++;
              }
              if($check == strlen($stringtoreplace)){
                  $n=0;$sub='';
                  for($n=0;$n&lt;=strlen($stringtoreplace);$n++){    
                  $str[$l] = ''; 
                      $sub .= $str[$l];
                      $l++;
                  }
                  $add .= $stringreplaceby;
                  $i += $check;
              }
          }
  
          $i++;
      }//echo $add;exit;
      return $add;
  }
  </code></pre>
  
  <p>I am getting output as helzzworld .
  Please take a look what I did wrong or if you have better solution for this please suggest. </p>