PHP Matching With Greedy Vs. Nongreedy Expressions – 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 "Nongreedy Replace: $nongreedyresult\n";
?>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.