PHP Replace With Regular Expression – String Function

<?php
  $teststring = '"Hello" and "Goodbye."';  $greedyresult = preg_replace('/".*"/', '"***"', $teststring);  $nongreedyresult = preg_replace('/".*?"/', '"***"', $teststring);
  echo "Original: $teststring\n";
  echo "Greedy Replace: $greedyresult\n";
  echo "Non-Greedy Replace: $nongreedyresult\n";
?>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.