Retrieve number from another number in php - php

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.

Related

php do while loop, display plus sign between results

I would want to display a plus sign between my do while loop result as illustrated below:
If i get two results i would want it to display as e.g. 46+56 and if three results 45+76+89.
Where do i place the plus sign + so that it does not display at the ends?
<?php do { ?>
+ <?php echo $row_studentcat['marks']; ?>
<?php } while ($row_studentcat = mysql_fetch_assoc($studentcat)); ?>
The code above would display result as +45+56 but i would want it to display as 45+56
Any help..
You should use the ltrim() function instead:
echo ltrim($s,'+');
Otherwise, rtrim() will do the same right hand side
For removing + sign from starting (left side), you can use ltrim() function:
Example:
$Yourstring = '+45+10';
echo ltrim($Yourstring,'+'); //45+10
Second param of Function ltrim() will remove the starting + from your string.
From PHP Manual:
ltrim — Strip whitespace (or other characters) from the beginning of a
string
I would implode the data so I can store the data from the other columns in other variables.
$marks = array();
while($row = mysql_fetch_assoc($studentcat)) {
$marks[] = $row['marks'];
}
echo implode('+', $marks);
This is very simple my friend, use php JOIN()...
<?php
$marksArray= array()l
while ($row_studentcat = mysql_fetch_assoc($studentcat))
{
$marksArray[] = $row_studentcat['marks'];
}
echo join('+',$marksArray);
?>
The faster way is concatenating the elements into a string and delete the first + using this method:
substr($string, 1);
Comparing with implode is 3 times faster and comparing with ltrim is a little bit faster because we only change the index array in this case, which is faster than remove an element
Furthermore, I would like to comment you two important issues of this code:
In case mysql_fetch_assoc returns you an empty result you will have an error, because you will try to access to an unexisting index array, better use while(){}
The method mysql_fetch_assoc is deprecated and is removed in PHP7, your code will have problems in the future: http://php.net/manual/es/function.mysql-fetch-assoc.php

PHP converting variable string 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 ));
}

PHP -- Convert string to bigint

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

Pulling a number and prefix from filename using PHP

I am currently using the following function to grab the product code number for a filename such as "62017 THOR.jpg"
$number = (int) $value;
Leaving me with 62017
The trouble is some of these files have prefixes which need to be left in place ie "WST 62017.jpg"
So im after
WST 62017
not
62017
Could someone help me, either redo what im using or alter ?
replace all characters except the numbers from the image name and get only numbers.
$number = preg_replace("~[^0-9]~", "", $value);
If you want to capture everything before the number and the number as well, you can use:
$value = "WST 62017.jpg";
$number = preg_replace('/^(.*?\d*)\..*/',"$1",trim($value));
// $number is "WST 62017"
See it
You could do it like this:
$value = preg_replace('/^(.*\d+).*$/', '\1', $filename);
It should replace everything after the first numeric value with nothing, leaving everything in front of it in place. Note that you wont't be able to cast the number to int, then.

PHP -- int typecast of numeric string value giving 0

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!

Categories