Calling all regex guru's - need to get embeded value within html string

This is an area, that for me is like hieroglyphics.

I have this variable that delivers a value of

$product_price = '<span class="foxee_product_price">$150.00</span>';

where I need to get the value ‘150.00’ less the html and dollar sign.

Wondering if someone could assist.

Thanks

None needed. :wink:


$product_price = '<span class="foxee_product_price">$150.00</span>';
list($price) = sscanf($product_price, '<span class="foxee_product_price">$&#37;s</span>');
echo $price; #150.00

Heres another way, using different functions - just in case your given example does not typify all your cases:


echo str_replace( "$", "" , strip_tags($product_price) ) ;

Not as cool as Anthony’s though, I’ll admit, but some more tools for your toolbox, eh?

Sometimes Regex’s are the right thing to use of course.

Did you have any more examples?

Thank you very much!

Off Topic:

I actually prefer yours… :cool:

For what it’s worth, your code won’t work as expected (well, kudos if it does but it shouldn’t). The %s will happily munch up the rest of the string, meaning that $price will actually contain150.00</span>

Instead, %f could be used, which would also convert the 150.00 into a float rather than a string (whether that’s a good or bad thing depends on the intended use of the value).

Cups’ solution is nice and simple, though I would’ve gone for an ltrim rather than str_replace but my initial thought when reading the question was to clear away the tags with strip_tags too. :eye:

Hi Salathe,

Could you explain why you think it shouldn’t please? I think I’ve correctly used the function for the purpose it was provided… I think. :stuck_out_tongue:

Cheers!

Anthony.