curl -> Xpath -> 返回一个字符串,需要进一步解析才能返回一个 3-4 位数字



这可能是一个非常愚蠢的问题,因为我是PHP的新手。基本上,我试图为我维护的竞争网站创建一些不错的"自动化"。

<?php
$url = "http://www.pdga.com/player/73761";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DomXPath($dom);
$class = 'current-rating';
$divs = $xpath->query("//*[contains(concat(' ', normalize-space(@class), '
'), ' $class ')]");
foreach($divs as $div) {
echo $div->nodeValue;
}
?>

此目前返回

Current Rating: 949 (as of 06-Dec-2016)

我想进一步解析它,以便给出数字949(在这种情况下)。

有效地尝试做的是每月这些评分更新,我的网站将在此数字更改时自动更新。另外,此数字的范围从750-1050范围,因此需要在此处考虑。

谢谢您提前的帮助。

您可以尝试使用preg_match()使用正则表达式匹配您想要的东西。

,例如

foreach($divs as $div) {
    preg_match('/Current Rating:s+(d+)/', $div->nodeValue, $results);
    echo $results[1];
}

最新更新