fork download
  1. <?php
  2.  
  3. $re = '~^(?<code>\S+)\s+(?<name>.*?)\s+(?<num>\$\d[\d.]*)\s*(?<details>.*)$~';
  4. $str = 'BK0001 My book (4th Edition) $49.95 (Clearance Price!)';
  5. if (preg_match($re, $str, $m)) {
  6. echo "Code: " . $m["code"] . "\nName: " . $m["name"] . "\nPrice: " .
  7. $m["num"] . "\nDetails: " . $m["details"];
  8. }
Success #stdin #stdout 0s 82624KB
stdin
Standard input is empty
stdout
Code: BK0001
Name: My book (4th Edition)
Price: $49.95
Details: (Clearance Price!)