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 ));
}
Related
sorry if my question was stupid, please someone help me to fix this issue.
i have string like
$str_value = "http://99.99.99.99/var/test/src/158-of-box.html/9/";
this $str_value is dynamic , it will change each page. now i need to replace 9 in this string as 10. add integer 1 and replace
for example if the $str_value = "http://99.99.99.99/var/test/src/158-of-box.html/251/"
then output should be
http://99.99.99.99/var/test/src/158-of-box.html/252/
i tried to replace using preg_match but i m getting wrong please somesone help me
$str = preg_replace('/[\/\d+\/]/', '10',$str_value );
$str = preg_replace('/[\/\d+\/]/', '[\/\d+\/]+1',$str_value );
Thank's for the answer, #Calimero! You've been faster than me, but I would like to post my answer, too ;-)
Another possibilty is to fetch the integer by using a group. So you don't need to trim $matches[0] to remove the slashes.
$str_value = "http://99.99.99.99/var/test/src/158-of-box.html/9/";
$str = preg_replace_callback('/\/([\d+])\//', function($matches) {
return '/'.($matches[1]+1).'/';
}, $str_value);
echo $str;
You need to use a callback to increment the value, it cannot be done directly in the regular expression itself, like so :
$lnk= "http://99.99.99.99/var/test/src/158-of-box.html/9/";
$lnk= preg_replace_callback("#/\\d+/#",function($matches){return "/".(trim($matches[0],"/")+1)."/";},$lnk); // http://99.99.99.99/var/test/src/158-of-box.html/10/
Basically, the regexp will capture a pure integer number enclosed by slashes, pass it along to the callback function which will purge the integer value, increment it, then return it for replacement with padded slashes on each side.
I'd suggest also another approach based on explode and implode instead of doing any regexp stuff. In my opinion this is more readable.
$str_value = "http://99.99.99.99/var/test/src/158-of-box.html/11/";
// explode the initial value by '/'
$explodedArray = explode('/', $str_value);
// get the position of the page number
$targetIndex = count($explodedArray) - 2;
// increment the value
$explodedArray[$targetIndex]++;
// implode back the original string
$new_str_value = implode('/', $explodedArray);
Weird situation and I'm not even sure what to call it (hence unable to find any previous posts).
I'm passing in a variable that is a string as function, and then attempting to concat it to another string as such:
function saveFunction($number){
$myStr = "drawer-$number[]";
return $myStr;
}
So the output should be :
saveFunction("2")
"drawer-2[]"
However, because it thinks I am accessing $number as an array because of the brackets, the output is:
"0"
I even tried this:
function saveFunction($number){
$myStr = "drawer-$number";
return $myStr + "[]";
}
And got the same result.
Suggestions?
Double quotes in PHP are tricky. They interpret the string. This is why "drawing-$number" works in the first place.
Sometimes your string doesn't lend itself for automatic interpretation. You can use the always safe concatenation:
return "drawer-" . $number . "[]";
Or use {} to help the automatic detection:
return "drawer-{$number}[]"; // as opposed to: "drawer-{$number[]}"
Or use sprintf:
return sprintf("drawer-%s[]", $number); // %s because $number is actually a string, not an int
You can just concat the variable with the string like this:
<?php
function saveFunction($number){
return $myStr = "drawer-" . $number . "[]";
}
echo saveFunction("2");
?>
Output:
drawer-2[]
For further information see also: http://php.net/manual/en/language.operators.string.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.
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
I have number like 9843324+ and now I want to get rid of + at the end and only have 9843324 and so how should I do this in php ?
Right now I am doing $customer_id = explode('+',$o_household->getInternalId); also $o_household->getInternalId returns me 9843324+ but I want 9843324, how can I achieve this ?
Thanks.
if you just want to pop the + off the end, use substr.
$customer_id = substr($o_household->getInternalId, 0, -1);
or rtrim
$customer_id = rtrim($o_household->getInternalId, "+");
You can use a regeular expression to remove anything that isn't a number
$newstr = preg_replace("/[^0-9]+/","",$str);
$customer_id = rtrim ( $o_household->getInternalId, '+' );
rtrim function reference
rtrim removes the characters in the second argument (+ only in this case) from the end of a string.
In case there is no + at the end of the string, this won't mess up your value like substr.
This solution is obviously more readable and faster than, let's say preg_replace too.
you could use intval($o_household->getInternalId)
Is there a reason you need to use explode? i would just use substr as Andy suggest or str_replace. Either would work for the example you provided.
You can use intval to get the integer value of a variable, if possible:
echo intval($str);
Be aware that intval will return 0 on failure.