开发者

cast integer php - remove symbol from string

I have this code and i make a cast to remove the symbol .

    $t = "€200开发者_C百科0";
    $venc = (int)$t;
    echo $venc; // actually echo is 0 and i want 2000 (remove symbol)

The output is 0 and not 2000, so, the code is not working as i expect.

What is the reason for (int)$t; not echo 2000 ?

thanks


casting routine does not remove invalid characters, but start from the beginning and stops when first invalid character is reached then convert it to number, in your case Euro sign is invalid and it is the first character thus resulting number is 0.

check http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion

you could try (int)preg_replace('/\D/ui','',$t);

however if you are dealing with currencies you should not forget that they are not integers but floats

(float)preg_replace('/[^0-9\.]/ui','',$t);


This can help you

$t = preg_replace('/[^0-9]/i', '','€2000');
$venc = (int)$t;
echo $venc;


$t = "€2000";
$venc = (int)substr($t,1);
echo $venc;


use substr

   echo substr($t,3);  // returns "2000"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜