dsagzmosl32217092 2015-01-06 12:48
浏览 56
已采纳

用于从html文件中检索jquery函数的正则表达式无法在PHP服务器上运行

I am using regex code to find jquery functions used inside any html file. The php code is:

<?php
$file=<<<FOO
<html>
<body>
    <div class="content">
        <div class="main-content">
            <ul class="show-sport">
                <li class="active"><img src="assets/img/show-1.png"/></li>
                <li><img src="assets/img/show-2.png"/></li>
                <li><img src="assets/img/show-3.png"/></li>
            </ul>
        </div>
    <script>
        $('.show-sport').fadeSlider({speed:5000});
        $('.modal-switch').modal();
    </script>
    </div>
</body>
</html>
FOO;
preg_match_all("/\$\('(.*)'\).(.*)\((.*)\)/", $file, $jQuery_func);
var_dump($jQuery_func);
?>

The $file will store file contents later on. This is output I am getting:

array (size=4)
  0 => 
    array (size=0)
      empty
  1 => 
    array (size=0)
      empty
  2 => 
    array (size=0)
      empty
  3 => 
    array (size=0)
      empty

But when I am testing it on any other online php regex tester, it is giving:

Array
(
    [0] => Array
        (
            [0] => $('.show-sport').fadeSlider({speed:5000})
            [1] => $('.modal-switch').modal()
        )

    [1] => Array
        (
            [0] => .show-sport
            [1] => .modal-switch
        )

    [2] => Array
        (
            [0] => fadeSlider
            [1] => modal
        )

    [3] => Array
        (
            [0] => {speed:5000}
            [1] => 
        )
)

You can see the results here http://www.phpliveregex.com/p/9fo, click preg_match_all tab.

PHP version: 5.4.16 | Apache Version: 2.4.4

I have hit my head many times but can't set it working on localhost. Is there any PHP extension that needs to be enabled to get it working?

  • 写回答

1条回答 默认 最新

  • dpd20130 2015-01-06 14:51
    关注

    just made some debug

    you have different regex in your php than on your online link page :-) (or I don't know why :-) it must be different)

    so just change your regex pattern from:

    preg_match_all("/\$\('(.*)'\).(.*)\((.*)\)/", $file, $jQuery_func);
    

    to

    preg_match_all("/\\$\('(.*)'\).(.*)\((.*)\)/", $file, $jQuery_func);
    

    or

    $pattern = "/\\$\('(.*)'\).(.*)\((.*)\)/";
                    var_dump($pattern);
                        preg_match_all($pattern, $file, $jQuery_func);
    

    to see why we should escape dollar sign twice

    it works :-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题