来自某些HTML代码的preg_match

时间:2012-09-08 01:57:50

标签: php html regex parsing preg-match

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

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

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

我已经尝试了几个小时才能做到这一点,我无法让它工作lol

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

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

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

您可以看到结果 here

以下是解释:

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 “<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 “</span>” literally «span>»