How can I display the SubTotal on OpenCart on any page?

寵の児 提交于 2019-12-23 19:53:30

问题


Currently the only global PHP command I know is:

<?=$text_items?>

This spits:

1 item(s) - £318.75

I want to get the 318.75 value so at the moment I am trying a replace but it is not working all smoothly:

$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:


$total = @floatval(end(explode('£', html_entity_decode($text_items))));
  • html_entity_decode changes &pound; to £
  • end(explode('£' is giving you string after '£' character
  • finally floatval is valuating string to float.
  • @ is bypassing E_STRICT error which occurs to passing constant in end() function.

Working example


Second solution is Regexp:

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

Working example



来源:https://stackoverflow.com/questions/12095789/how-can-i-display-the-subtotal-on-opencart-on-any-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!