简体   繁体   中英

strip_tags + html entities to get only numbers

I would like to remove everything but the amount as a float from this string:

<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">&#8362;</span>700.00</span>

I tried:

  1. strip_tags( $total_price_paid ); - Not enough.
  2. strip_tags( html_entity_decode( $total_price_paid ) ); - it decodes the entity into a symbol, i tried preg_replace after and it didn't work.
  3. preg_replace( '/[^0-9]/', '', $value ); - Doesn't get rid of html entity

None of those achieved a result of 700.00 formatted as a float.

Can anyone help please?

Thanks!

You need to remove also the special pieces of text used to define entities , so you need at least another pass:

$total_price_paid = strip_tags($total_price_paid);
$total_price_paid = preg_replace("/&#?[a-z0-9]{2,8};/i", "", $total_price_paid); 

Code snippet is available here .

$str = '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">₪</span>700.00</span>';

echo floatval(substr($str, stripos($str, "</span>")+7, strripos($str, "</span>")+7));

If you want to use preg_match then you can use like that:

$string = '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">&#8362;</span>700.00</span>';

preg_match('/\d+\.\d{1,2}/', $string, $matches);

echo $matches[0]; // 700.00

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM