如何在任何页面上显示OpenCart上的SubTotal?

时间:2012-08-23 16:03:57

标签: php str-replace opencart substr

目前我所知道的唯一全局PHP命令是:

<?=$text_items?>

这吐了:

1 item(s) - £318.75

我想获得318.75值,所以在我尝试更换时,但它并没有顺利运行:

$short = $text_items;
$short = str_replace("£", "", $short);
$short = str_replace("&pound;", "", $short);
$short = str_replace("-", "", $short);
$short = str_replace("&ndash;", "", $short);
$short = str_replace(" ", "", $short);
$short = str_replace("-", "", $short);
$short = str_replace("ITEMS", "", $short);
$short = str_replace("(", "", $short);
$short = str_replace(")", "", $short);
$short = str_replace("item(s)", "", $short);
$short = str_replace("ITEM", "", $short);

1 个答案:

答案 0 :(得分:2)

$total = @floatval(end(explode('£', html_entity_decode($text_items))));
  • html_entity_decode&pound;更改为£
  • end(explode('£'在'£'字符
  • 之后为您提供字符串
  • 最后floatval正在评估字符串浮动。
  • @绕过了在E_STRICT函数中传递常量时发生的end()错误。

Working example


第二个解决方案是Regexp:

preg_match_all('!\d+(?:\.\d+)?!', $text_items, $result);
echo $result[1];

Working example