Advertisement
evilbloodydemon

Untitled

Feb 17th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.24 KB | None | 0 0
  1. <?php
  2. function print_r_reverse($in) {
  3.     $lines = explode("\n", trim($in));
  4.     if (trim($lines[0]) != 'Array') {
  5.         // bottomed out to something that isn't an array
  6.         return $in;
  7.     } else {
  8.         // this is an array, lets parse it
  9.         if (preg_match("/(\s{5,})\(/", $lines[1], $match)) {
  10.             // this is a tested array/recursive call to this function
  11.             // take a set of spaces off the beginning
  12.             $spaces = $match[1];
  13.             $spaces_length = strlen($spaces);
  14.             $lines_total = count($lines);
  15.             for ($i = 0; $i < $lines_total; $i++) {
  16.                 if (substr($lines[$i], 0, $spaces_length) == $spaces) {
  17.                     $lines[$i] = substr($lines[$i], $spaces_length);
  18.                 }
  19.             }
  20.         }
  21.         array_shift($lines); // Array
  22.         array_shift($lines); // (
  23.         array_pop($lines); // )
  24.         $in = implode("\n", $lines);
  25.         // make sure we only match stuff with 4 preceding spaces (stuff for this array and not a nested one)
  26.         preg_match_all("/^\s{4}\[(.+?)\] \=\> /m", $in, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
  27.         $pos = array();
  28.         $previous_key = '';
  29.         $in_length = strlen($in);
  30.         // store the following in $pos:
  31.         // array with key = key of the parsed array's item
  32.         // value = array(start position in $in, $end position in $in)
  33.         foreach ($matches as $match) {
  34.             $key = $match[1][0];
  35.             $start = $match[0][1] + strlen($match[0][0]);
  36.             $pos[$key] = array($start, $in_length);
  37.             if ($previous_key != '') $pos[$previous_key][1] = $match[0][1] - 1;
  38.             $previous_key = $key;
  39.         }
  40.         $ret = array();
  41.         foreach ($pos as $key => $where) {
  42.             // recursively see if the parsed out value is an array too
  43.             $ret[$key] = print_r_reverse(substr($in, $where[0], $where[1] - $where[0]));
  44.         }
  45.         return $ret;
  46.     }
  47. }
  48.  
  49. $result = file_get_contents("http://www.hobbycenter.ru/API/product.php?login=EOORXYTFNG&article=TRA6407&code=43&key=4708d4f1435b473b5ca74b87679f5cb1");
  50. $result = substr($result, strpos($result, 'Array'));
  51. var_dump($result);
  52. var_dump(print_r_reverse($result));
  53. die();
  54. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement