简体   繁体   English

PHP如何合并2个查询

[英]PHP How to merge 2 queries

I have these 2 queries: 我有以下两个查询:


1) Get all the URL of the images: 1)获取图像的所有URL:

$imgs = $xpath->query('//div[@class="pin"]/div[@class="PinHolder"]/a/img');



2) Get how many people facebook-liked every image: 2)获得多少人喜欢每个图片的Facebook:

foreach($xpath->query('//span[@class="LikesCount"]') as $span) {
    $int = (int) $span->nodeValue;
    if ($int > 5) {
        echo $i++ . "--> " . $int . "<br />";
    }
}



I'd like to merge them to get just the images which has been facebook-liked more than 5 times That said, pictures that hasn't been liked don't have the LikesCount class at all. 我想将它们合并以获得仅Facebook赞过5次以上的图像。也就是说,未被喜欢的图片根本没有LikesCount类。

Follow an example of the Markup: 遵循标记的示例:

<div class="pin">

[...]

<a href="/pin/56787645270909880/" class="PinImage ImgLink">
    <img src="http://media-cache-ec3.pinterest.com/upload/56787645270909880_d7AaHYHA_b.jpg" 
         alt="Krizia" 
         data-componenttype="MODAL_PIN" 
         class="PinImageImg" 
         style="height: 288px;">
</a>

<p class="stats colorless">
    <span class="LikesCount"> 
        22 likes 
    </span>
    <span class="RepinsCount">
        6 repins
    </span>
</p>

[...]

</div>

To retrieve not all images, but only images with a likes-count of 5 or more, I'd try changing the XPath expression in the assignment to $imgs to read: 要不检索所有图像,而只检索“喜欢”计数为5或5以上的图像,我尝试将分配给$imgs的XPath表达式更改为:

//div[@class="pin"]
     [.//span[@class = 'LikesCount']
             [substring-before(normalize-space(.),' ') > 5]]
     /div[@class="PinHolder"]
     /a/img

(I have added whitespace to make this a little easier to follow; you may need to eliminate the newlines if your XPath parser doesn't follow the spec in this matter [some don't]). (我添加了空格,使它更易于理解;如果您的XPath解析器在此问题上未遵循规范,则可能需要消除换行符。)

It's not clear to me why cbuckley says this will require XPath 2.0; 我不清楚cbuckley为什么说这将需要XPath 2.0。 perhaps he sees some subtle issue here that I don't. 也许他在这里看到了一些我不知道的微妙问题。

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

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