我有這段代碼,我用cast
刪除了符號€
。cast integer php - 從字符串中刪除符號
$t = "€2000";
$venc = (int)$t;
echo $venc; // actually echo is 0 and i want 2000 (remove symbol)
輸出爲0
,而不是2000
,因此,代碼不工作,我期望的那樣。
(int)$t;
not echo 2000是什麼原因?
感謝
我有這段代碼,我用cast
刪除了符號€
。cast integer php - 從字符串中刪除符號
$t = "€2000";
$venc = (int)$t;
echo $venc; // actually echo is 0 and i want 2000 (remove symbol)
輸出爲0
,而不是2000
,因此,代碼不工作,我期望的那樣。
(int)$t;
not echo 2000是什麼原因?
感謝
鑄造例程不會刪除無效字符,而是從一開始就開始,當達到第一個無效字符,然後將其轉換爲數字,你的情況歐元符號是無效的停止,它是第一個字符從而得到的數字是0
檢查http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion
你可以嘗試(int)preg_replace('/\D/ui','',$t);
但是如果你正在處理的貨幣你不應該忘記自己是不是整數,但浮
(float)preg_replace('/[^0-9\.]/ui','',$t);
這可以幫助你
$t = preg_replace('/[^0-9]/i', '','€2000');
$venc = (int)$t;
echo $venc;
使用substr
echo substr($t,3); // returns "2000"
返回此¬2000 – user773961 2011-06-14 15:53:42
strlen of euro標誌是3 ;-) – Codebeat 2013-11-11 20:32:22
$t = "€2000";
$venc = (int)substr($t,1);
echo $venc;
方式比我的if/else/index_of /更優雅is_numeric方法大聲笑 – sjobe 2011-06-14 15:50:30