3

php preg_match() を php に記述して 250 の値を取得するにはどうすればよいでしょうか。250 を選択したい html コードの大きな文字列があり、正規表現を正しく取得できないようです。

これは、一致させたい 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 に答える