简体   繁体   中英

Regular expression extract a JavaScript variable in PHP

I have a large HTML file, containing a lot of content. I want to get a JavaScript variable, named 'a' for example, from the whole file.

Example: (deleted lots of the actual content)

<html>
    <head>
        <script>
            var a = [{'a': 1, 'b': 2}];
        </script>
    </head>
    <body>
        ....
    </body>
</html>

What should come from the above is:

[{'a': 1, 'b': 2}]
preg_match('#var a = (.*?);\s*$#m', $html, $matches);
echo $matches[1];

Explanation:

  • Regex will try to match any line containing var a =
  • It will then match everything up until a ; , any amount of spaces \\s* , then the end of the line $
  • The m modifier will try to match each line independently, without it, the $ would just match then end of the string which would be a bit useless

The any amount of spaces is only there in case you have some spaces after the definition, no other reason (eg human error). If you're sure that won't happen, you can remove \\s* .

Note that this doesn't replace a full-blown parser. You will need to make modifications if a is defined over more than one line, if a is defined more than once (think about scope, you can have var a on a global scope, then var a within a function), etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM