Regular expression extract a JavaScript variable in PHP - Stack Overflow

I have a large HTML file, containing a lot of content. I want to get a JavaScript variable, named '

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 e from the above is:

[{'a': 1, 'b': 2}]

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 e from the above is:

[{'a': 1, 'b': 2}]
Share Improve this question edited Aug 22, 2013 at 18:05 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Jul 7, 2012 at 21:16 NovakNovak 2,76810 gold badges46 silver badges64 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12
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 (e.g. 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743848846a4517603.html

相关推荐

  • Regular expression extract a JavaScript variable in PHP - Stack Overflow

    I have a large HTML file, containing a lot of content. I want to get a JavaScript variable, named '

    1天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信