Advertisement
dimipan80

Price List

May 1st, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.88 KB | None | 0 0
  1. <?php
  2. $priceList = $_GET['priceList'];
  3. $priceList = str_replace("\r\n", "", $priceList);
  4. $pattern = "/\s*<tr>\s*<td>\s*([^<]+)\s*<\/td>\s*<td>\s*([^<]+)\s*<\/td>\s*<td>\s*([0-9.]+)\s*<\/td>\s*<td>\s*(\w+)\s*<\/td>\s*<\/tr>\s*/";
  5.  
  6. preg_match_all($pattern, $priceList, $matches, PREG_SET_ORDER);
  7. $productList = array();
  8. foreach ($matches as $match) {
  9.     $match[1] = html_entity_decode($match[1]);
  10.     $match[2] = trim(html_entity_decode($match[2]));
  11.     $match[3] = html_entity_decode($match[3]);
  12.     $match[4] = html_entity_decode($match[4]);
  13.  
  14.     $product = array('product' => $match[1], 'price' => $match[3], 'currency' => $match[4]);
  15.  
  16.     if (!array_key_exists($match[2], $productList)) {
  17.         $productList[$match[2]] = array();
  18.     }
  19.  
  20.     array_push($productList[$match[2]], $product);
  21. }
  22.  
  23. ksort($productList);
  24. foreach ($productList as &$category) {
  25.     sort($category);
  26. }
  27.  
  28. echo json_encode($productList);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement