- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
伙计们,我正在解析 URL 以获取 HTML dom 元素。
这是我的代码:
<?PHP
$url = 'http://www.sportsdirect.com/nike-satire-mens-skate-shoes-242188?colcode=24218822';
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTMLFile($url);
$xp = new DOMXPath($dom);
$qry = '//script[starts-with(normalize-space(.), "var colourVariantsInitialData")]';
$rawtxt = $xp->query($qry)->item(0)->nodeValue;
$jsonStart = strpos($rawtxt, '[');
$jsonEnd = strrpos($rawtxt, ']');
$collections = json_decode(substr($rawtxt, $jsonStart, $jsonEnd - $jsonStart + 1));
foreach ($collections[1]->SizeVariants as $item) {
$SizeName = $item->SizeName;
$PriceUnformated = $item->ProdSizePrices->SellPrice;
$find = array('£');
$replace = array('');
$Price = str_replace($find, $replace, $PriceUnformated);
echo "SizeName: <b>$SizeName</b> - Price: <b>$Price</b><br>";
}
此代码从输出源的脚本中获取“文本”。这是此脚本的完整文本:http://pastebin.com/FwK9Z8CP
我的代码给出了以下结果:
SizeName: 7 (41) - Price: 27.00
SizeName: 8 (42.5) - Price: 36.00
SizeName: 9 (44) - Price: 36.00
SizeName: 9.5 (44.5) - Price: 36.00
SizeName: 11 (46) - Price: 36.00
我的问题是:
我如何才能只获得特定 SizeName 的结果,例如 SizeName 7 (41)?
提前致谢!
最佳答案
$specific
有您要查找的字符串。将代码中的 foreach
更改为:
$specific = '7 (41)';
foreach ($collections[1]->SizeVariants as $item) {
$SizeName = $item->SizeName;
if(trim($SizeName) == trim($specific)) {
$PriceUnformated = $item->ProdSizePrices->SellPrice;
$find = array('£');
$replace = array('');
$Price = str_replace($find, $replace, $PriceUnformated);
echo "SizeName: <b>$SizeName</b> - Price: <b>$Price</b><br>";
}
}
关于php - DOMXPath/PHP - 仅在特定出现后获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29377087/
这个问题在这里已经有了答案: final keyword in method parameters [duplicate] (9 个回答) 关闭 8 年前。 在此示例中,声明 Object fina
我的目标:是通过我的函数更新字段获取选定值并使用函数输出值运行它。 问题:当我从列表中选择值时,它不会触发函数,也不会更新字段。 感谢您的帮助。 HTML 12 14 16 18 20 22 24
我有一本具有这种形式的字典: myDict = {'foo': bar, 'foobar baz': qux} 现在,我想拆分字典键中的空格,使其成为下一个键并获取值(重复)。 myDictRev1
vector a; vector b; int temp_holder; cout > temp_holder) a.push_back(temp_holder); cout > temp_h
Java 的开发过程中免不了与 Date 类型纠缠,准备总结一下项目经常使用的日期相关操作,JDK 版本 1.7,如果能够帮助大家节约那么几分钟起身活动一下,去泡杯咖啡,便是极好的,嘿嘿。当然,我
我正在使用 jquery ui 日期选择器来获取 fromDate 和 toDate 以下是from日期的代码 $("#from_date").datepicker({
我是一名优秀的程序员,十分优秀!