3

How would I write a php preg_match() in php to pick out the 250 value. I have a large string of html code that I want to pick the 250 out of and I can't seem to get the regular expression right.

This is the html pattern I want to match - note that I want to extract the integer where the 250 is:

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

I have been trying for hours to do this and I can't get it to work lol

2 Answers 2

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

The regex actually depends on your code. Where are you exactly searching for?

Sign up to request clarification or add additional context in comments.

5 Comments

I literally want to pull any number that comes after H$ where it is surrounded by that specific pattern with the class="price-ld"
This gets a match: //get the title of the product preg_match('/<title>(.*)<\/title>/i', $candidate[0], $title); $p_title = $title[1];
But this one doesnt: //get the price of the product preg_match('/<span class="price-ld">H$(\d+)<\/span>/i', $candidate[0], $price); $p_price = $price[1]; echo $p_price;
I wonder why your example mentions that new title tag. Maybe you need some whitespace tolerance. Minify your regexp like /class="price-ld"\s*>\s*H$(\d+)/. Anyway this forces no more HTML comment to be inbetween the tag content and that class. It depends on the page you want to screen scrape what kind of parsing you can take. You should post an excerpt somewhere (pastebin.com)
Anyway maybe you should obey the common tip Don't use regexp to parse HTML, use a real XML parser.
1

This is the regex you're looking for:

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

You can see the results here.

And here's the explanation:

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>»

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.