Is there a simple way to remove a leading zero (as in 01 becoming 1)?
You can use the ltrim function:
ltrim($str,"0");
$str = "01";
echo intval($str);
if you use the trim functions, you might mistakenly remove some other character, like by triming "12" your will have "2".
use the intval() function. this function will convert your string (which could start by a leading zero or not) to an integer value. intval("02") will be 2 and intval ("32") wll be 32.
Regex replace /^0*/ with '' for a string return solution
The exact code will be something like this
<?php
$string_number = '000304';
echo preg_replace('/^0*/', '', $string_number);
?>
Just multiply by 1
echo "01"*1
Related
In my store i am trying to change a String into an integer the code which I am using is:
<?php echo $_excl; ?>
<?php $int_excl = intval($_excl);?>
<?php var_dump ($int_excl);?>
When i var_dump $int_excl is says the value is 0 when it should be 4.99 .
What am I doing wrong?
var_dump ($_excl) returns string(33) "£4.99"
Both floatval and intval methods start to look for numbers from the beginning of the string (removing the preceding whitespace) - and stop looking at the first symbol that cannot be a part of the number. In your case, it's the very first one - £. That's why intval just returns 0 (as floatval does).
One way to resolve this is to remove all the non-digit symbols at the beginning of your string with preg_replace:
$excl = '£4.99';
$value = preg_replace('/^\D+/u', '', $excl);
var_dump(intval($value)); // int(4)
var_dump(floatval($value)); // float(4.99)
Demo. This approach is change-resistant - it works (without any modifications) if £ is replaced with $ or any other currency symbol, or dropped altogether.
<?php
$_excl = '4.99' ;//assuming
echo $_excl;
$int_excl = floatval($_excl);
var_dump ($int_excl);
?>
Use above code, since $_excl = 4.99 which is a float value, intval() would return 4.
This should work for you:
(You have to remove the character £ from the string)
<?php
$_excl = "£4.99";
$_excl = str_replace("£", "", $_excl);
$int_excl = floatval($_excl);
var_dump ($int_excl);
?>
Output:
float(4.99)
You are parsing float values, and therefore need floatval() PHP Float Manual.
If intval() returns 0, it means that the conversion failed. Source: PHP Intval)
In your case, the conversion fails because the string doesn't start with a number, but with a currency symbol.
This should solve your problem:
<?php
echo $_excl;
$int_excl = floatval(ltrim($_excl, '£'));
var_dump ($int_excl);
?>
(ltrim removes the first character if it is £)
I am trying to write a regular expression such that if a number have decimal point then the zeros (0) at the end must be removed.
Example:
$value = 234.8076000
After Regexp Replace it should become
234.8076
I am trying the following regexp [0]+$ in preg_replace but the problem is that if the value does not have decimal point and it contain zero at the end then that zero is also removed.
Example:
$value = 2340
It becomes 234 but it should remain 2340
Any idea? Is there any in-built function in php that can do this?
Yes, you really can do it with regex:
$pattern = '/(\.\d*[^0])0+$/';
echo preg_replace($pattern, '$1', '2340'); // 2340
echo preg_replace($pattern, '$1', '2340.0'); // 2340.0
echo preg_replace($pattern, '$1', '2340.07600'); // 2340.076
... but the simplest way is just convert a string value into a float value.
echo (float)'2340'; // 2340
echo (float)'2340.0'; // 2340
echo (float)'2340.07600'; // 2340.076
Echoing floats that are really integer values drops the decimal part apparently - but it seems from your comments it's actually what you want.
You can do it without regexpes:
php > echo ((float)"21.40200")."\n";
21.402
/(\.\d*?)0+$/, works except /\d+\.0{n}/ cases
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 ));
}
I have this code and i make a cast to remove the symbol €.
$t = "€2000";
$venc = (int)$t;
echo $venc; // actually echo is 0 and i want 2000 (remove symbol)
The output is 0 and not 2000, so, the code is not working as i expect.
What is the reason for (int)$t; not echo 2000 ?
thanks
casting routine does not remove invalid characters, but start from the beginning and stops when first invalid character is reached then convert it to number, in your case Euro sign is invalid and it is the first character thus resulting number is 0.
check http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion
you could try (int)preg_replace('/\D/ui','',$t);
however if you are dealing with currencies you should not forget that they are not integers but floats
(float)preg_replace('/[^0-9\.]/ui','',$t);
This can help you
$t = preg_replace('/[^0-9]/i', '','€2000');
$venc = (int)$t;
echo $venc;
$t = "€2000";
$venc = (int)substr($t,1);
echo $venc;
use substr
echo substr($t,3); // returns "2000"
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.