PHP string functions – substr_replace and str_replace

This tutorial explains how to use the substr_replace function to replace part of a string with something else. It also explains how to use the str_replace function to replace multiple occurrences of a portion of a string with another string value. Watch the video below and then scroll down to see the sample code.

Sample PHP code:

<?php
  // substr_replace() replaces part of a string with something else
  $mystring = "The quick brown dog jumps over the lazy dog";
  echo substr_replace($mystring,"cat",16,3) , "</br>";

  // str_replace() replaces occurrences of a portion of a string with another value
  echo str_replace("dog","rat",$mystring);
?>