我有几个字符串已从另一个网站使用cURL提取。该字符串包含整个页面的HTML结构,但每一个页面里面有概述如下段落:PHP - 从字符串中提取值
<p>Displaying 1-15 of 15 items beginning with A</p>
<p>Displaying 1-20 of 33 items beginning with B</p>
我需要做的只是从这些字符串中提取的总价值(15
在上述情况下为33
)。
我不确定提取值的最佳方法是什么。
谢谢:)
我有几个字符串已从另一个网站使用cURL提取。该字符串包含整个页面的HTML结构,但每一个页面里面有概述如下段落:PHP - 从字符串中提取值
<p>Displaying 1-15 of 15 items beginning with A</p>
<p>Displaying 1-20 of 33 items beginning with B</p>
我需要做的只是从这些字符串中提取的总价值(15
在上述情况下为33
)。
我不确定提取值的最佳方法是什么。
谢谢:)
蛮力方法:
http://php.net/manual/en/function.preg-match-all.php
preg_match_all('/<p>Displaying (\d+)-(\d+) of (\d+) items beginning with ([A-Z]+)</p>/', $subject, $matches);
谢谢:)作品像一个魅力:) – lethalMango 2011-06-10 13:12:11
没问题,总是在你的服务。 – 2011-06-10 13:14:38
创建一个正则表达式;
$regex = "/Displaying 1-([0-9]+) of ([0-9]+) items begginning with/";
preg_match($regex,$resultfromcurl,$match);
是这样的吗?
非常感谢你,+1 – lethalMango 2011-06-10 13:12:32
可能会迟到一天,但是这里是我的2美分:这将解析文件中的html,抓取段落,找到匹配,并将所有相关值放入数组中使用。
<?php
// Open your document
$doc = new DOMDocument();
// Parse the HTML
$doc->loadHTMLFile("html_doc.html");
// Find the paragraphs and loop through them
$paras = $doc->getElementsByTagName('p');
// Initialize value array
$range = array();
// Extract the value and put them in a useful data structure
for ($i = 0; $i < $paras->length; $i++) {
$subject = $paras->item($i)->nodeValue;
preg_match('/Displaying (\d+)-(\d+) of (\d+) items beginning with ([A-Z]+)/', $subject, $matches);
$range[$matches[4]] = array(
'start' => $matches[1],
'stop' => $matches[2],
'total' => $matches[3]
);
}
foreach ($range as $begin => $values) {
echo "\n$begin\n";
echo "start: " . $values['start'] . "\n";
echo "stop: " . $values['stop'] . "\n";
echo "total: " . $values['total'] . "\n";
echo "------\n";
}
我敢打赌,有人会很快提供一个答案,使用正则表达式来提取总数 – andyb 2011-06-10 12:57:44
哦,真的吗?我刚刚做到了。 :) – 2011-06-10 12:58:46
@Tomasz赫赫,感谢您验证我的理论:-) +1为你。 – andyb 2011-06-10 13:03:05