Home  >  Article  >  Backend Development  >  Parse JS code function (parse JS variables into PHP arrays)

Parse JS code function (parse JS variables into PHP arrays)

WBOY
WBOYOriginal
2016-07-25 09:01:261458browse

Useful when collecting.

  1. /*
  2. Parse JS code function
  3. JS code such as:
  4. var pp_link="/videos/41939vod-play-id-41939-(ppvod).html";var pp_vodname="chill";
  5. Parse output:
  6. Array(
  7. [pp_link] => "/videos/41939vod-play-id-41939-(ppvod).html"
  8. [pp_vodname] => "Chill"
  9. )
  10. */
  11. function parse_js($string ){
  12. $pregString="#var ([a-zA-Z_0-9]+)=([^;]*);#";
  13. preg_match_all($pregString,$string,$JsArrayPre);
  14. $num= count($JsArrayPre['0']);
  15. for($i=0;$i<$num;$i++){
  16. $jsVarName=$JsArrayPre['1'][$i];
  17. $JsArray[$ jsVarName]= $JsArrayPre['2'][$i];
  18. }
  19. return $JsArray;
  20. }
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn