I have the following string which i need to convert to integer or bigint.
$test="99999977706";
I tried:
echo (int)$test;
echo (integer)$test;
echo intval($test);
But they are all returning me 2147483647.
How do i convert the above string to a number or bigint?
Many many thanks for all suggestions.
MySQL isn't going to know the difference. Just feed the string into your query as though it is a number without first type casting it in PHP.
For example:
$SQL = "SELECT * FROM table WHERE id = $test";
working solution :
<?php
$str = "99999977706";
$bigInt = gmp_init($str);
$bigIntVal = gmp_intval($bigInt);
echo $bigIntVal;
?>
It will return : 99999977706
In your snippet $test is already a number, more specifically a float. PHP will convert the contents of $test to the correct type when you use it as a number or a string, because the language is loosely typed.
For arbitrary length integers use GMP.
You can treat the string as string without converting it.
Just use a regex to leave only numbers:
$test = "99999977706";
$test = preg_replace('/[^0-9]/', '', $test);
It can help you,
$token= sprintf("%.0f",$appdata['access_token'] );
you can refer with this link
Related
I am programming in php and I have the following variable :
$calculation = '15-12';
Is there a function allowing me to convert the character string into calculation?
Yeah, you can use eval expression for such case, but it's not recommended (mention by #El_Vanja). It would be better to cast the values to their correct types and do the calculation.
eval scenario
$calculation = '15-12';
$result = eval('return '.$calculation.';');
print $result; // output will be 3
casted types
$calculation = '15-12';
$values = explode('-', $calculation);
$result = intval($values[0]) - intval($values[1]);
print $result;
The eval() function could be used here:
$calculation = '15-12';
$result = eval('return '.$calculation.';');
echo $result; //outputs '3'
You can use eval() https://www.php.net/manual/en/function.eval
Pay attention that this function execute the string like PHP code and if it contains specific mathematical expression, it will fail. In this case you'll need to parse your string and do calculation by yourself or use an external library.
#aspirinemaga's solution for casting is the approach that I would take.
If it isn't always going to be subtraction (and you want to avoid using eval()) then you could always use strpos to figure out if it is adding/subtracting/multiplying and then pass it the expression to a switch statement to get your calc.
I got this result from mysql query using php:
chris123
I want to separate chris from 123 because I am going to use the value of int for operation.
$char=string('chris123');
$int=int('chris123');
How am I able to do it in PHP? Not PDO. Not so familiar with it. Thanks.
If you know the non-number parts are always going to be lowercase characters, you can do this:
$str = 'chris123';
$num = trim($str, 'a..z');
If you also want it to be an actual integer, you can cast it:
$str = 'chris123';
$num = (int) trim($str, 'a..z');
I'm trying to retrieve 4119850115 from the below number but i could not extract it properly
<?php
$num=1000014119850115;
if(strlen($num)>6){
$num = substr(($num), 6);
}
echo $num;
?>
the above code gives me 4119850100
but when i assign value to $num as below
$num='1000014119850115';
i get 4119850115 which is what i wanted but,
the number is generated dynamically and when i try to add single quotes to it like
$num=1000014119850115;
$num="'".$num."'";
even tried strval() and (string) to convert
its not working it gives 4119850100
can any one help me out with this . was checking this code in codepad.
thanks in advance.
In PHP you don't have to put single or double quotes around a number.
If you put quotes PHP considers a string.
Go to see the PHP documentation about the type juggling, I think it will resolve your problem.
Try this and see if it's works :
echo (int)$num;
echo (string)$num;
Try this:
$num = '1000014119850115';
$num = sunstr ($num,6);
$num = intval ($num);
Get substring first then convert it to int.
To start, I could not find this answer online because of the way my variable string is defined. Normally I should be able to add 0 to the variable, or use (int), but it does not work.
<?php
$casestringid = "'118'";
$caseid = $casestringid + 0;
echo $casestringid;
echo $caseid;
?>
Output: '118'0
As you can see, because of the way my first variable is declared, the standard methods of converting a string to an integer does not work. My $casestringid is written like that because it requests a number from another page. Rather than trying to change how to format that, I figure it will be easier for help on how to convert a string that looks like that, into an integer. I would like the output of caseid to be 118. Thanks for any help in advance.
The problem is that '118' is not an integer as far as the PHP parser is concerned, it's a string. It looks like an integer to us, of course, but it has slashes (') which make it "unconvertible".
Use str_replace for this:
intval(str_replace("'", '', $casestringid));
i think you have no other chance like this:
intval(str_replace("'",'',$casestringid));
Replace the '':
intval(str_replace("'",'',$casestringid));
Try intval ($casestringid) + 0.
EDIT:
How about this, then:
filter_var ($casestringid, FILTER_SANITIZE_NUMBER_INT);
You have to remove the single quotes and use intval().
<?php
$casestringid = "'118'";
$parseid = str_replace("'", "", $casestringid);
$caseid = intval($parseid);
echo $casestringid;
echo $caseid;
?>
$casestringid = "'118'";
$int = str_replace("'", "", $casestringid);
echo intval($int);
If it is just an integer you are looking for, this could work.
it will remove any non digit characters then return it as an int
function parseInt( $s ){
return (int)(preg_replace( '~\D+~' , '' , $s ));
}
OK, so there is a page I'm querying on another server that returns a comma separated list of two values. Something it would return would be:
850,640
I have some PHP code that calls file_get_contents on that page and needs to do some numeric calculations based on the two values.
No matter what I try, I can't seem to get an int value out of this.
$res = trim(file_get_contents('http://thatURL/'));
echo "X" . $res . "X<br/>";
list($x,$y) = array_map(create_function('$a', 'return (int)$a;'), explode(',', $res));
echo "X:$x";
results in the output:
X 850,640 X
X:0
Note the spaces before and after the comma separated values(how the hell? I trim'd them!) and that $x is assigned the value 0.
What am I doing wrong here?
What am I doing wrong here?
Nothing, as far as I can see, which indicates that the content of $res is not quite what you expect. Could you change the first echo to:
echo htmlentities($res);
My guess is $res contains some un-printed characters, for example, it is actually:
<span> </span>850,640<span> </span>
or
850,640
Try the following. The array_map and llamda function are arguably overkill for your usage.
$res = " 850,640 ";
echo "X" . $res . "X<br/>";
list($x,$y) = explode(',', trim($res));
echo "X:" . (int)$x;
echo "Y:" . (int)$y;
Worked for me, but I'm not using file_get_contents(). If that doesn't work, something else is being output by the page.
PHP is not a typed language. Use intval to convert a string to integer.
Correction: it is a loosely typed language! That's what I meant!
Since I was using file_get_contents() on a URL, there was some HTML being put in as well that I didn't notice in my echo because it parsed out... just empty body and html tags. Oops!