PHP preg_filter 返回意外的长值 [英] PHP preg_filter returns unexpected long value

查看:18
本文介绍了PHP preg_filter 返回意外的长值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在 Woocommerce 中去除标签和过滤值,但无法以正确的格式获取它.东西有点腥..

Trying to strip tags and filter value in Woocommerce but can't manage o get it in the correct format. Something is fishy..

我使用 WC()->cart->get_cart_subtotal(); 来检索值.在此示例中,我的值为 2,429kr,原始返回值为 <span class="amount">2,429kr</span>

Am using WC()->cart->get_cart_subtotal(); to retrieve the value. In this example my value is 2,429kr and the raw returned value is <span class="amount">2,429kr</span>

$cart_total = WC()->cart->get_cart_subtotal();
$cart_total_format = strip_tags($cart_total);
$cart_value = preg_filter("/[^0-9,.]/", "", $cart_total_format);

echo $cart_value;

结果 = 2,429107114
预期 = 2,429

Result = 2,429107114
Expected = 2,429

我不是 PHP 向导,所以我认为我做错了什么,并尝试了几种不同的方法和方法,但没有得到正确的结果.

Am not a PHP wizard so i thought i was doing something wrong and did try several diffren't approches and methods without getting the correct result.

然后我确实尝试将 WC()->cart->get_cart_subtotal(); 的原始输出作为 $string

Then i did try to run the raw out output from WC()->cart->get_cart_subtotal(); as a $string

$string_total = '<span class="amount">2,429kr</span>';
$string_total_format = strip_tags($string_total);
$string_value = preg_filter("/[^0-9,.]/", "", $string_total_format);

echo $string_value;

结果 = 2,429
预期 = 2,429

Result = 2,429
Expected = 2,429

为什么?:(

更新

在 Woocommerce @Github 中挖掘时发现了这个

Found this when digging around in Woocommerce @Github

case 'SEK' : $currency_symbol = '&#107;&#114;'; break;

所以真正的价值是:

<span class="amount">2,429&#107;&#114;</span>

现在的问题是过滤掉它的最佳方法是什么?我的快速修复方法是这样的,虽然不美观但可以解决问题.

Question now is what the best approach to filter this out ? My quick fix approach is this, it's not beautiful but fix the issue.

$cart_total = WC()->cart->get_cart_subtotal();
$cart_total_format = strip_tags($cart_total);
$cart_value = preg_filter("/[^0-9,.]/","", $cart_total_format);
$cart_value_new = preg_filter("/107114/",".", $cart_value);

echo $cart_value_new;

结果 = 2,429
预期 = 2,429

Result = 2,429
Expected = 2,429

推荐答案

好的,这就是正在发生的事情.get_cart_subtotal() 返回一个 HTML 编码的字符串.因为你不是在查看实际的源代码,而是 var_dump-ing 它并查看你看到的 HTML <span class="amount">2,429kr</span>,实际上k"和r"被编码为它们等效的 HTML 实体(基于它们的 ASCII 代码)、&#107;&#114.

Ok, so this is what is happening. get_cart_subtotal() returns an HTML-encoded string. Because you are not looking at the actual source, but rather var_dump-ing it and looking at the HTML you are seeing <span class="amount">2,429kr</span>, when in fact the "k" and "r" are encoded into their equivalent HTML entities (based on their ASCII codes), &#107; and &#114.

这也是为什么 var_dump 显示 string(45) "2,429kr" 实际上应该返回 string(7) "2,429kr" 的原因code> 如果货币没有被编码(并且 标签没有被浏览器解释).

That is also why var_dump shows string(45) "2,429kr" when it should in fact return string(7) "2,429kr" if the currency weren't encoded (and the <span> tags weren't interpreted by the browser).

然后,当您应用 preg_filter 时,它当然也包括来自 HTML 实体的数字,因为它们与正则表达式匹配.

Then, when you apply the preg_filter, it also includes the numbers from the HTML entities, of course, because they match the regular expression.

所以最简单的解决方案是过滤之前解码所有HTML实体:

So the easiest solution is to decode all HTML entities before filtering:

$cart_total = html_entity_decode($cart_total, ENT_QUOTES, 'UTF-8');

所以你的代码变成:

$cart_total = WC()->cart->get_cart_subtotal();
$cart_total = html_entity_decode($cart_total, ENT_QUOTES, 'UTF-8');
// rest of your code

这篇关于PHP preg_filter 返回意外的长值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆