dongzanxun2790 2018-06-14 15:33
浏览 72

PHP正则表达式仅在文本中的某个单词后重复匹配[重复]

This question already has an answer here:

that's my first question here. :) Was searching around with my problem for a few days, but it is not yet fully solved. What I have is a bunch of text. There is some price data divided by exact phrase "promoted-after" . So here is my RegEx:

'/price-([\d $гр€\.]*)/i'

It awesomely works for ALL the prices it founds including prices before divider. But when I modify it to:

'/promoted-after.*price-([\d $гр€\.]*)/is'

It correctly bypasses the top part, but then saves only one last price of all the data. How can it be modified to correctly save only all the prices AFTER "promoted-after" tag? Here is the example of input:

price- 2680 $
a lot of some random html code here
price- 3250 $
a lot of some good html code here
price- 3450 $
promoted-after
price- 400 $
a lot of some strange html code here
price- 401 $
a lot of some awesome html code here
price- 402 $
a lot of some ugly html code here
price- 403 $
a lot of some nice html code here
price- 404 $
a lot of some best html code here

P.S. I use preg_match_all

EDIT: Ok, let's just ignore that it's HTML. Let it be plain text. What is the overall logical construction behind such a task should be?

</div>
  • 写回答

1条回答 默认 最新

  • douweng1935 2018-06-14 16:12
    关注

    As an alternative you might use DOMDocument and DOMXPath and use an xpath expression to find the div with the id promoted-after and then find all the siblings p/strong.

    You could get their value using nodeValue.

    $dom = new DOMDocument();
    $dom->loadHTML($data);
    $xpath = new DOMXPath($dom);
    $items = $xpath->query('//div[@id="promoted-after"]/following-sibling::p/strong');
    foreach($items as $item) {
        echo $item->nodeValue . "<br>";
    }
    

    Result

    400 $
    401 $
    402 $
    403 $
    404 $
    

    Demo

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭