Advertisement
Mayur_Pipaliya

PHP - json parser & crawler

Oct 2nd, 2012
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.34 KB | None | 0 0
  1. <?php
  2.  
  3. // Functions...
  4.  
  5. // Serialize native Javascript object to JSON. To quote the key names. key=>'key'
  6. function fix_json( $j ){
  7.   $j = trim( $j );
  8.   $j = ltrim( $j, '(' );
  9.   $j = rtrim( $j, ')' );
  10.   $a = preg_split('#(?<!\\\\)\"#', $j );
  11.   for( $i=0; $i < count( $a ); $i+=2 ){
  12.     $s = $a[$i];
  13.     $s = preg_replace('#([^\s\[\]\{\}\:\,]+):#', '"\1":', $s );
  14.     $a[$i] = $s;
  15.   }
  16.   //var_dump($a);
  17.   $j = implode( '"', $a );
  18.   //var_dump( $j );
  19.   return $j;
  20. }
  21.  
  22.  
  23. # iCutter Function for PHP 5.x :) -  snap particular string from another STRING starting from X and ending at Y.
  24.  
  25. $CHKVER = stripos(phpversion(), "5.3");
  26. if ($CHKVER === false)
  27. {
  28.     function snap($start,$end,$string)
  29.     {
  30.     // For PHP <= 5.2
  31.     $search = array(stristr($string,$end),$start);
  32.     $snap = str_ireplace($search,"",stristr($string,$start));
  33.     return $snap;  
  34.     }
  35. }
  36. else
  37. {
  38.     function snap($start,$end,$string)
  39.     {
  40.     // For PHP >= 5.3
  41.     $portion = str_ireplace($start,'',stristr( stristr($string,$start ,false), $end,true));
  42.     return $portion;
  43.     }
  44. }
  45.  
  46. function isExist($str,$key)
  47. {
  48. $pos1 = stripos($str, $key);
  49. if ($pos1 === false)
  50.     return false;
  51. else
  52.     return true;
  53. }
  54.  
  55. function getJson($str)
  56. {
  57.     if(isExist($str,'faredetails'))
  58.         return snap('.faredetails=','};',$str) . '}';
  59.     if(isExist($str,'legdetails'))
  60.         return snap('.legdetails=','}]}',$str) . '}]}';
  61.     if(isExist($str,']='))
  62.         return snap(']=','}',$str) . '}';
  63.      return null;
  64. }
  65.  
  66. // Actual Code Starts from here....
  67.  
  68.  
  69. $data = file_get_contents("http://prismatric.com/keentripo/AirTran.php");
  70. $data = snap("var isFlexiSearch",';;',$data);
  71. $data_arr = explode("blrmaa_1",$data);
  72.  
  73. // Total Flights..
  74. $_tot_flight = snap('[',']',$data_arr[count($data_arr)-1]) - 23;
  75. echo '<pre>';
  76.  
  77.  
  78. $j=0;
  79. for($i=count($data_arr)-1;$i>=$_tot_flight;--$i)
  80. {
  81.     $json = fix_json(trim(getJson($data_arr[$i])));
  82.     if($json != null)
  83.     {
  84.         if(isExist($json,'index'))
  85.         {
  86.          $_arr[$j]['index'] = $json;
  87.          $j++;
  88.         }
  89.         if(isExist($json,'leg'))
  90.          $_arr[$j]['leg'] = $json;
  91.         if(isExist($json,'fare'))
  92.          $_arr[$j]['fare'] = $json;
  93.     }
  94. }
  95.  
  96. // $_arr have all flight details in multi-dimentional array + json format
  97.  
  98.   $jd =  json_decode(($_arr[0]["index"]));      // 1st flight's 'index'
  99.  echo $jd->{'index'};
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  var_dump($jd);
  110.  var_dump($_arr);
  111.  
  112.  
  113. // Pratik Gothaliya & Mayur Pipaliya
  114. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement