Guest User

Untitled

a guest
Jul 22nd, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. <?php
  2.  
  3. // Does not handle anything tricky very well. It does make scraping super simple on
  4. // certain sites, however.
  5. function parseJsVars($code)
  6. {
  7. // Doesn't properly handle semicolons in strings.
  8. $code = preg_replace("/[\r\n]+/", "", $code);
  9. $chunks = preg_split('/\s*;\s*/', $code);
  10. $vars = array();
  11.  
  12. foreach($chunks as $c) {
  13. if (preg_match('/^var ([^=]+)\s*=\s*(.+)/', $c, $match)) {
  14. list(, $name, $value) = $match;
  15.  
  16. if (!in_array($value[0], array('{', '[', '"', "'"))) {
  17. if (!is_numeric($value[0])) {
  18. continue;
  19. }
  20. }
  21. $vars[trim($name)] = json_decode($value);
  22. }
  23. }
  24. return $vars;
  25. }
Add Comment
Please, Sign In to add comment