3

我将如何在 php 中编写一个 php preg_match() 来挑选 250 值。我有一大串 html 代码,我想从中挑选 250 个,但我似乎无法正确获取正则表达式。

这是我要匹配的 html 模式 - 请注意,我要提取 250 所在的整数:

<span class="price-ld">H$250</span>

我已经尝试了几个小时来做​​到这一点,但我无法让它工作哈哈

4

2 回答 2

3
preg_match('/<span class="price-ld">H$(\d+)<\/span>/i', $your_html, $matches);
print "Its ".$matches[1]." USD";

正则表达式实际上取决于您的代码。你到底在哪里寻找?

于 2012-09-08T02:03:00.843 回答
1

这是您正在寻找的正则表达式:

(?<=<span class="price-ld">H\$)\d+(?=</span>)

你可以在这里看到结果。

这是解释:

Options: case insensitive; ^ and $ match at line breaks

Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=<span class="price-ld">H\$)»
    Match the characters “&lt;span class="price-ld">H” literally «<span class="price-ld">H»
    Match the character “$” literally «\$»
Match a single digit 0..9 «\d+»
    Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=</span>)»
    Match the characters “&lt;/span>” literally «span>»
于 2012-09-08T08:39:10.357 回答