<?php
header('Content-type: text/html; charset=gb2312');
include("parser.php");
$html = file_get_html('http://www.souyishop.com/shop_gkq5/1280/products.aspx?sku=1351249&shbid=20358');
$price = $html->find('span[id=Product_main1_Label5]');
$price = str_replace(" ", "", str_replace("¥", "", str_replace("元", "", $price[0])));
echo (int)$price . "<br>";
?>
基本上,我试图获取价格 ¥ 32 元,我删除了货币和空格。之后,我尝试将字符串转换为 int,以便稍后进行计算。你猜怎么着,我得到 0。:(
最佳答案
像这样的东西是行不通的:
echo intval('¥ 32');
所以尝试替换所有非数字
echo (int)preg_replace( '~\D~', '', $str );
关于php - 如何将字符串转换为整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5747046/