Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here's an example page of what I'm trying to scrape...

[
[4056, 1, 'Saturday, Aug 18 2012', '15:00', 171],
[4057, 1, 'Saturday, Aug 18 2012', '15:00', 94],
[4058, 1, 'Saturday, Aug 18 2012', '15:00', 175],
[4059, 1, 'Saturday, Aug 18 2012', '15:00', 29],
[4051, 1, 'Saturday, Aug 18 2012', '15:00', 13],
[4053, 1, 'Saturday, Aug 18 2012', '15:00', 170],
[4055, 1, 'Saturday, Aug 18 2012', '17:30', 23],
[4060, 1, 'Sunday, Aug 19 2012', '13:30', 194],
[4054, 1, 'Sunday, Aug 19 2012', '16:00', 16],
[4052, 1, 'Monday, Aug 20 2012', '20:00', 31],
[4123, 1, 'Wednesday, Aug 22 2012', '19:45', 15]
]

Here's the code I'd normally use for something similar...

$str = file_get_contents('http://www.thewebsite.com/info/');
$jsonarray = json_decode($str, true);

$id = $jsonarray['id'];

But as you can see... that this isn't the same. Also, I don't even know if that first page is in JSON (it's a JS Array right?).

How would I scrape the individual items from this for putting into variables/array?

share|improve this question
add comment

4 Answers

up vote 1 down vote accepted

The data isn't valid JSON because it uses single quotes instead of double quotes to wrap strings.

If you know that the content will not contain other single quotes, other than the string wrappers then you could simply replace single with double quotes to correct the JSON format.

$fixedJSON = str_replace("'", '"', $str);
$data = json_decode($fixedJSON);

foreach($data as $row)
{
    echo $row[2]; // Saturday, Aug 18 2012
}

If the content could contain single quotes other than the string wrappers then it it will be harder to correct the JSON because you will need to only replace the wrapping quotes and handle quotes within the data which may or may not be escaped. For example double quotes will need escaping and single quotes will need the escaping removed.

share|improve this answer
 
Yes.. It'll be like this every time and your solution works. Thanks for keeping it simple <3 –  Cully Jul 16 '13 at 11:46
1  
No problem :) happy coding! –  MrCode Jul 16 '13 at 12:14
add comment

Sorry, an ugly solution :\ you can use str_replace also

<?php
  $str  = preg_replace( "/'(.+?)'/", '"$1"', $str );
  var_dump( json_decode( $str ) );
?>
share|improve this answer
add comment

It should be:

$str = file_get_contents('http://www.thewebsite.com/info/');
$jsonarray = json_decode($str, true);

$id = $jsonarray[0][0]; // 4056
$id1 = $jsonarray[1][0]; // 4057
// etc
share|improve this answer
 
But the data is not JSON, so json_decode won't work! –  Felix Kling Jul 16 '13 at 11:41
 
@Matyunin I tried this, but it doesn't work.. Doesn't pick up anything. –  Cully Jul 16 '13 at 11:44
1  
Yes, data in question is not a valid JSON, we need to use $fixedJSON = str_replace("'", '"', $str); like in first answer. –  Matyunin Jul 16 '13 at 12:42
add comment

Keep the array in a variable.....

  var jsonarray = json_decode($str, true);

  var len = jsonarray.length
  for (var i=0; i<len; ++i) {
       var s = myStringArray[i];
  }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.