php - 如何在任何页面上显示 OpenCart 上的小计?

标签 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);

最佳答案

$total = @floatval(end(explode('£', html_entity_decode($text_items))));
  • html_entity_decode£ 更改为 £
  • end(explode('£' 在 '£' 字符之后给你字符串
  • 最后 floatval 将字符串赋值为 float 。
  • @ 正在绕过在 end() 函数中传递常量时发生的 E_STRICT 错误。

Working example


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

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

Working example

关于php - 如何在任何页面上显示 OpenCart 上的小计?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12095789/

相关文章:

javascript - 如何选择html表中的所有数据并使用ajax将其发送到我的数据库

php - 打印订阅还剩多少天?

javascript - jQuery 序列化数据未成功发布到 PHP 表单处理程序

php - 使用生成表中的 PHP 代码更新 SQL 表

Opencart 3.x 新主题未显示在管理面板中?

php - str_replace 和 preg_replace 尝试?

php - 如何使用 php 将字符串中的最后一个逗号替换为 "and"?

php - 纯文本上的简单 HTML DOM str_replace

.htaccess - 我只想在 opencart 结帐页面中使用 https,其余页面应该有 http

opencart - 什么是 open-cart 中的 mvcl 设计模式?