Cast integer php - remove a character from a string

I have this code, and I'm doing castto delete a character .

    $t = "€2000";
    $venc = (int)$t;
    echo $venc; // actually echo is 0 and i want 2000 (remove symbol)

Conclusion 0, not 2000, so the code does not work as I expect.

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

thank

+3
source share
4 answers

the casting procedure does not delete invalid characters, but starts from the beginning and stops when the first invalid character is reached, and then converts it to a number, in your case, the Euro character is invalid and is the first character, the result of which is 0.

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

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

, , , ,

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

+8

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

substr

   echo substr($t,3);  // returns "2000"
0
$t = "€2000";
$venc = (int)substr($t,1);
echo $venc;
0

All Articles