Search code examples
phpregexpreg-match

PHP Regex Match Selected Pattern inside Div


Hi i have large string in which it contains price i want to fetch the price string with price.

here is my code

$value=preg_match_all('/<div class=\"_productInfo\">([^`]*?)<\/div>/',$data,$estimates);

string contain many Div

<div class"_productInfo"> 2,200 Price </div> 

i want to match the string in this pattern and get data inside div


Solution

  • You have a spelling issue in your regex. You wrote _prouctInfo instead of _productInfo

    $value = preg_match_all('/<div class=\"_productInfo\">\s+(([0-9,.]*?) Price)\s+<\/div>/', $data, $estimates);
    

    preg_match_all stored the found contents in the array $estimates. You can grab it via $estimates[1][0] for the first subpattern match 2,000 Price and $estimates[2][0] holds only the price 2,000.