# how to convert string to int in php



## eke21 (Jun 27, 2003)

Hi, I have a php script that will get a line coming from a file. my problem is that, I have to compare the two values out of the lines. example :

line 1 --> I have 2,000 dollars.
line 4 --> my dollars = 1,000.00 only

i have to manipulate the values in the line, let's say 2000/1000 which is equal to 2.

how can i extract the numbers in the line and convert it to integer so that mathematical operations is possible? 

thanks in advance!


----------



## john1704 (Feb 20, 2009)

All you have to do is use the intval function:

```
$realval = intval("5"); // now 5
```
The problem is that I don't know if PHP knows how to get rid of all of those characters, so you might have to extract the numbers with a regular expression. The method below guarantees you will only get numbers.

```
$savings = "I have $2,000 dollars.";
$savingsval = intval(preg_replace('/\D/', '', $savings));
$total = 1000 + $savingsval;
echo $total; // prints 3000
```


----------



## Big-K (Nov 22, 2003)

You'll have to use the regular expression. PHP can convert any type of data to any other type of data, but it has special rules for each. Converting a string directly to an integer will grab any numbers at the start of the string and use those, then truncate the rest. If the first character isn't a number then you get 0.


----------



## MMJ (Oct 15, 2006)

> how to convert string to int in php


php converts by itself so you don't have to do anything.

i dont understand the rest of your post


----------



## deazy86 (Jan 14, 2009)

Hey check out this site you will find all the information you want phpf1.com.


----------

