【问题标题】:How can I display the SubTotal on OpenCart on any page?如何在任何页面上显示 OpenCart 上的 SubTotal?
【发布时间】:2012-08-23 16:03:57
【问题描述】:

目前我知道的唯一全局 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);

【问题讨论】:

  • 有没有办法直接获取变量,而不必像这样通过文本解析?

标签: php str-replace opencart substr


【解决方案1】:
$total = @floatval(end(explode('£', html_entity_decode($text_items))));
  • html_entity_decode&amp;pound; 更改为 £
  • end(explode('£' 在 '£' 字符之后给你字符串
  • 终于floatval 正在评估字符串以浮动。
  • @ 正在绕过 E_STRICT 错误,该错误是在 end() 函数中传递常量时发生的。

Working example


第二种解决方案是正则表达式:

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

Working example

【讨论】:

  • 我只想要值318.75 没有井号,没有破折号。就是那个值。基本上是英镑英镑符号后的值。
  • 是的,所以我给了你两个解决方案。你甚至检查过我的例子吗?
  • 谢谢。什么是快速和更好用的?你能解释一下正则表达式吗?
  • 我向您推荐第一种解决方案。正则表达式只是在字符串中寻找两位小数和点.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多