繁体   English   中英

PHP Preg_Match_All返回无结果

[英]PHP Preg_Match_All Returning No Results

我正在使用Simple HTML Dom Parser从网页中抓取脚本标签,然后尝试使用preg_match_all()解析来自该标签的某些数据。 但是,当我打印preg_match_all时,没有结果返回。

以下是我正在使用的代码:

<head>
    <?php
        require_once "toolkit/http.php";
        require_once "toolkit/web_browser.php";
        require_once "toolkit/simple_html_dom.php";
    ?>
</head>
<body>
    <?php

        $prod_url = 'http://www.domain.com/subpage.html';
        $html = file_get_html($prod_url);
        $script = $html->find('script', 17);
        //echo $script;
        preg_match_all('(?<=\d":)\w++', $script, $matches);
        print_r($matches);

    ?>
</body>

我可以看到HTML Simple Dom代码可以正常工作,因为在回显$script变量时可以获得预期的结果。 结果是:

<script type="text/javascript">
var PRODUCT_JSON = {
    "Def":{
        "default":202705111,
        "Listing:[{
            "label":"Includes",
            "options":[
                {label:"All", id: "884"},
                {label:"None", id: "485"},
            ]
        }],
        "Lookup":{
            "1":202705111,
            "0":202493236
        }
        }
};
</script>

因此,问题似乎出在我在preg_match_all()中使用的正则表达式上。 正则表达式的目标是在脚本标签的结尾附近返回两个数字202705和202493。 尽管我也试过了preg_match_all('\\(?<=\\d\\":\\)\\w++', $script, $matches);但结果相同preg_match_all('\\(?<=\\d\\":\\)\\w++', $script, $matches);这可能与转义双引号或括号有关。关于我在做什么错的任何想法吗?

如果您忘记定界符,可以使用T-Regx ,它会自动添加定界符。

$matches = pattern('(?<=\d":)\w++')->match($script)->all();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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