0

I am trying to fetch a price on a given page by using regular expressions, but the variable I use to store the fetched content is always empty. Can some one help me in writing the correct regular expression.

If the page is: http://www.flipkart.com/mobiles/memory-cards/itmczcsrtvjeb6nr?pid=acccrrqzzsgnfgea&_l=sXQjsX87GxqrvKzhjuOrkw--&_r=n_2yuAC4xgh0SZTuulvAtw--&ref=af8ad0c4-62a2-4381-99d3-3ad8285e260b

I want to fetch price 260 from here.

Some html code of page for tags:

<span id="fk-mprod-our-id" class="price final-price our fksk-our">Rs.<span class="small-font"> </span>260</span>

3 Answers 3

2

you could write more bulletproof parser using simplehtmldom - see http://simplehtmldom.sourceforge.net/. For me it never failed to parse document.

You will end up with code like this

<?php
include_once '/path/to/simplehtmldom/simple_html_dom.php';
$html = file_get_html('http://www.flipkart.com/mobiles/memory-cards/itmczcsrtvjeb6nr?pid=acccrrqzzsgnfgea&_l=sXQjsX87GxqrvKzhjuOrkw--&_r=n_2yuAC4xgh0SZTuulvAtw--&ref=af8ad0c4-62a2-4381-99d3-3ad8285e260b');
foreach ($html->find('span.final-price') as $element) {
    echo $element->plaintext;
}
//will output "Rs. 260", unless page changes

Much cleaner code, though it's performance nightmare when compared to regexes

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

Comments

0

Looks like that's the only time the final-price class is used, so this ought to work:

/final-price.+?>(\d+)</

Comments

0

Assuming that the Currency might change depending on the IP/Country, i would have used Explode (am not so good at Regex)

//consider that $html contains the page source
$html = explode('<span class="price final-price our fksk-our" id="fk-mprod-our-id">', $html);
$html = explode("</span>', $html[1]);
$price = $html[1];

i hope that helps.

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.