3

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

1 回答 1

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

于 2012-08-23T16:26:23.927 回答