Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Does not handle anything tricky very well. It does make scraping super simple on
- // certain sites, however.
- function parseJsVars($code)
- {
- // Doesn't properly handle semicolons in strings.
- $code = preg_replace("/[\r\n]+/", "", $code);
- $chunks = preg_split('/\s*;\s*/', $code);
- $vars = array();
- foreach($chunks as $c) {
- if (preg_match('/^var ([^=]+)\s*=\s*(.+)/', $c, $match)) {
- list(, $name, $value) = $match;
- if (!in_array($value[0], array('{', '[', '"', "'"))) {
- if (!is_numeric($value[0])) {
- continue;
- }
- }
- $vars[trim($name)] = json_decode($value);
- }
- }
- return $vars;
- }
Add Comment
Please, Sign In to add comment