|
|
#1 |
|
Beta Member
Join Date: Dec 2009
Posts: 4
|
Ok. I am trying to do a basic scrape and echo results via PHP.
I have the information I am trying to scrape, and I think I am using the correct code, but I am not sure, since it is not showing the array of the results when I try it. Here is a section of the page source I am trying to scrape Code:
noire","user_id":"3237825","show_in_sig":"1","show_in_profile":1,"last_engine_run":"1260190557","tap_count":"11984","view_count":"10951","total_gold_won":2119743,"env_health":"32166","env_bg_id":null,"env_last_grant_time":"1260190557","inhab_retire":false,"game_info":{"1":{"type":1,"instance_id":"1190571832.1260284818.446690843","open_time":1260292832,"close_time":1260293672,"end_time":1260293732,"length":60,"results_time":1260293742,"state":"open","player_count":6}},"events":
Here is the code I am using to do this... Code:
<?php
$data = file_get_contents('http://www.gaiaonline.com/chat/gsi/index.php?v=json&m=[[6500%2C[1]]%2C[6510%2C[%22789151%22%2C0%2C1]]%2C[6511%2C[%22789151%22%2C0]]%2C[6512%2C[%22789151%22%2C0]]%2C[107%2C[%22null%22]]]&X=1260293122');
$regex = '/"state":"(.+?)","player_count"/';
preg_match($regex,$data,$match);
var_dump($match);
echo $match;
?>
1. array(0) { } Array Completely not showing the results. I tried scraping another section using the above code, and it did work, but the section that did work did not have any " around it. I am VERY new at PHP, so I am figuring it is something to do with my $regex, and the whole " in the results I am looking for. |
|
|
|
|
|
#2 |
|
Site Team
Join Date: Mar 2004
Posts: 6,945
|
you might find that the quotes are literally just quotes,
so your $regex string might need to be Code:
'/"state":"(.+?)",":player_count"/'; but I'd recommend that you take the page source that you think that you are getting, (i.e with valid HTML, not just quotes). you then use the http://uk2.php.net/manual/en/functio...ars-decode.php html special chars decode function to remove all the HTML to plain text... then search for the plain text... (as shown in the code block that I'd changed above for your $regex string
__________________
I didn’t fight my way to the top of the food chain to be a vegetarian… Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns. "The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries." |
|
|
|
|
|
#3 |
|
Site Team
Join Date: Jul 2009
Posts: 2,629
|
I'm tired so might be off, but is your regex right? Agree with what root has said, so looking at the regex alone:
Code:
/"state":"(.+?)",":player_count"/ I'm not sure about escaping characters and suchlike specifically for php, but the regex I'd use would simply be: Code:
("state":"[^"]+")
|
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|