Mega Code Archive

 
Categories / Php / Strings
 

Matching with Greedy vs Nongreedy Expressions

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