fork download
  1. <?php
  2. $subjects = [
  3. "りんご 150円 5個で 500円 (値引き 250円)",
  4. "りんご 150円 3個で 450円 5個で 750円",
  5. "りんご 150円 3個で 400円 (値引き 50円) 5個で 500円 (値引き 250円) (値引き 0円)(値引き 50円)(値引き 100円)(値引き 200円)"
  6. ];
  7.  
  8. foreach ($subjects as $s) {
  9. preg_match("/^りんご (?<ringo_1>\d+)円(?: 3個で (?<ringo_3>\d+)円(?: \(値引き (?<nebiki_3>\d+)円\))?)?(?: 5個で (?<ringo_5>\d+)円(?: \(値引き (?<nebiki_5>\d+)円\))?)?.*$/", $s, $matches);
  10. unset($matches[0], $matches[1], $matches[2], $matches[3], $matches[4], $matches[5]);
  11. extract($matches);
  12. echo "{$ringo_1}, {$ringo_3}, {$nebiki_3}, {$ringo_5}, {$nebiki_5}\n";
  13. }
  14.  
  15.  
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
150, , , 500, 250
150, 450, , 750, 250
150, 400, 50, 500, 250