Skip to content

Instantly share code, notes, and snippets.

@dreanmer
Last active August 15, 2017 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreanmer/e49faca778a2cc0efc21fa2d688b6f80 to your computer and use it in GitHub Desktop.
Save dreanmer/e49faca778a2cc0efc21fa2d688b6f80 to your computer and use it in GitHub Desktop.
Fizz Buzz solution one if (with regex)
<?php
$result = '';
for($i=1;$i<=100;$i++){
if(!($i%3)){
$result .= 'm';
}
$result .= $i . "\n";
}
$result = preg_replace("/m\d*(0|5)\n/", "FizzBuzz\n", $result);
$result = preg_replace("/\d*(0|5)\n/", "Buzz\n", $result);
$result = preg_replace("/m\d+\n/", "Fizz\n", $result);
echo $result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment