在PHP中从preg_match_all中返回的值

问题描述 投票:0回答:1

我正在创建一个函数,从网站的页面源返回UA代码(来自Google Analytics)。

我创建了一个regex:'[U][A]-\d{5,10}-\d{0,2}]'用于UA代码。

我使用了preg_match_all,但似乎没有得到任何回报,而我知道我使用的网站至少有一个UA代码。

这是我目前所写的。

<?php

$url = 'www.ibood.com/nl/nl/';

$options = [
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_FOLLOWLOCATION => TRUE,
    CURLOPT_MAXREDIRS => 3
];

$init = curl_init($url);
curl_setopt_array($init, $options);
$site = curl_exec($init);

preg_match_all('/[U][A]-\d{5,10}-\d{0,2}]/', $site, $matches);

print $matches[0][0];

我只是得到一个白屏。

我在这里可能做错了什么?

php curl
1个回答
0
投票

使用这个RegEx /[U][A]-\d{5,10}-\d{0,2}/ 你的regex中多了一个钩子:) 之前。https:/regex101.comrDDdJhA1。 后。https:/regex101.comrUPA2iZ1


0
投票

你可以试试这个。

/[U][A]-([0-9]{5,10})-([0-9]{0,2})+/

总是尝试获取您想要解析的响应的样本片段,并使用诸如 https:/regexr.com

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.