strip_tags + html实体仅获取数字

时间:2019-06-19 12:34:59

标签: php regex

我想从此字符串中删除除浮点数以外的所有内容:

var viewModel = new ViewModel
{
    CelulaId = new SelectList(res.Where(r => r.Discriminator == "Celulas"), "Id", "Nome"),
    UAPId = new SelectList(res.Where(r => r.Discriminator == "UAP"), "Id", "Nome"),
    ReferenciaId = new SelectList(res.Where(r => r.Discriminator == "Referencias"), "Id", "Nome"),
    CelulaTipoId = new SelectList(res.Where(r => r.Discriminator == "CelulaTipos"), "Id", "Nome")
};

我尝试过:

  1. <span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">&#8362;</span>700.00</span> -不够。
  2. strip_tags( $total_price_paid );-它将实体解码为符号,我在之后尝试了preg_replace,但它不起作用。
  3. strip_tags( html_entity_decode( $total_price_paid ) );-不会摆脱html实体

这些都没有实现preg_replace( '/[^0-9]/', '', $value );格式为浮点数的结果。

有人可以帮忙吗?

谢谢!

3 个答案:

答案 0 :(得分:3)

您还需要删除用于定义实体的特殊文字,因此至少需要另一遍操作:

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

代码段可用here

答案 1 :(得分:1)

$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));

答案 2 :(得分:1)

如果要使用preg_match,则可以这样使用:

$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