php - Parsing a value from a JSON webpage -


using either javascript or php, how can value webpage? json attempting parse:

{"error":[""],"templatehtml":"", "_visitor_conversationsunread":"0","_visitor_alertsunread":"0"}

i trying value of "_visitor_alertsunread". how go doing this?

thank you!

you either parse using regex, using json decoding, or simple indexing. however, of these three, json clean , correct way go.

1) json decoding:

$page = file_get_contents($url); $json_arr = json_decode($string,true); return $json_arr['_visitor_alertsunread']; 

2) regular expression:

$page = file_get_contents($url); $pattern = ".*?_visitor_alertsunread\\\":\\\"(\\d)\\\""; preg_match($pattern, $page, $matches); return $matches[1]; 

3) indexing:

$page = file_get_contents($url); $needle = "_visitor_alertsunread"; $startpos = strrpos($page, $needle) + strlen($needle) + 3; $endpos = strrpos($page, "\"", $startpos); return substr($page, $startpos, $endpos); 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -