How to convert string with numbers and spaces to int

I have a little problem. I am trying to convert a string like "1,234" to a number: 1234 I can’t get there. The string is cleared from the website. Perhaps there is no place? Because I tried methods like str_replace and preg_split for space and nothing. Also (int) $ abc accepts only the first digit (1). If anyone has an idea, I would love to! Thank!

+3
source share
4 answers

Here's how I would handle it ...

<?php

$string = "Here! is some text, and numbers 12 345, and symbols !£$%^&";

$new_string = preg_replace("/[^0-9]/", "", $string);

echo $new_string // Returns 12345

?>
+8
source
intval(preg_replace('/[^0-9]/', '', $input))
+8
source

- , , , - , .

- str_replace.

$iInt = (int)str_replace(array(" ", ".", ","), "", $iInt);
+4
$str = "1 234";
$int = intval(str_replace(' ', '', $str)); //1234
+1

All Articles