如何在php中将字符串转换为整数

I have a variable $var1 = "21,855.00". I already tried using preg_replace and str_replace but I could not really achieve what I was looking for. I wish to echo it as 21855. How can I do that using PHP?

Just taking note:

If its a literal string like:

$var1 = '21,855.00'; // string(9) "21,855.00"

You can also do it this way:

$var1 = '21,855.00';
$var2 = filter_var(strtok($var1, '.'), FILTER_SANITIZE_NUMBER_INT);
echo $var2; // 21855

Sample Output

Or with regex:

$var1 = '21,855.00';
$var2 = (int) preg_replace('/(?<=\d),(?=\d{3}\b)/','', $var1);
var_dump($var2); // int(21855)

Use intval()

$var1 = 21855.00;
$varInt= intval($var1);

You can also do

$varInt= (int)$var1;

You can also use floor() to round down

$varInt= floor($var1);

You can also use ceil() to round up

$varInt= ceil($var1);

You can also do

settype($var1, "integer");

haha i must be having a lot of time to waste today :)

You can try with this also -

$var1 = (int) 21855.00;

You can do it in different ways;

$myFloat  = 123456.78;
$firstWay = intval($myFloat);
$secondWay= (int) $myFloat;

You can use intval() as Hanky stated or cast the value to integer with using (int). I believe using (int) $myFloat is faster than using intval($myFloat)

You could try intval() eg:

$val = 21855.00; 
$val_int = intval($val);