Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a string with numbers and spaces into an int

I have a small problem. I am tryng to convert a string like "1 234" to a number:1234 I cant't get there. The string is scraped fro a website. It is possible not to be a space there? Because I've tried methods like str_replace and preg_split for space and nothing. Also (int)$abc takes only the first digit(1). If anyone has an ideea, I'd be greatefull! Thank you!

like image 613
Gigg Avatar asked Jun 10 '11 14:06

Gigg


People also ask

Does parseInt ignore whitespace?

parseInt( ) The function returns an integer parsed from the given string. If the first argument is not a string, it is converted to one automatically using ToString() . Either way, any leading whitespace is ignored.

Can we convert string to integer?

We can convert String to an int in java using Integer. parseInt() method. To convert String into Integer, we can use Integer. valueOf() method which returns instance of Integer class.


1 Answers

This is 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

?>
like image 192
Dan Avatar answered Nov 15 '22 08:11

Dan