I have a simple code here using str_pad() function that will add 0 prefixes to a number. This is my code:
$num = 456;
$test = str_pad($num, 6, '0', STR_PAD_LEFT);
echo $test;
output:
000456
What if I want to reverse the code? If my number is set to $num = 000456, then it will output 456 only? Hoping for your kind consideration for my noob question. Thanks
Simply
$val = ltrim($your_num, "000");
$val = (int)$your_num;
Unless i am missing something, this should do the trick.
This helped me.
$str = "0011";
echo (int)$str;
and output is 11
Related
I have variable of type varchar having values $pointmoney=356+2311;
pointmoney is a field in database from where i am retreiving this value .
now i have separte two variable $point and $money
where i want to store $point=356 and $money=2311 from $pointmoney=356+2311.(i.e separate values from '+').
If any 1 knows any similar function then pls answer.
hope i ellaborated well to understand my query . if anything is unclear pls feel to comment .
Use explode():
$pointmoney = "356+2311";
list($point, $money) = explode('+', $pointmoney);
you explode function like
$pointmoney = "356+2311";
$arr_money = explode("+",$pointmoney);
echo $point = $arr_money[0];
echo $money= $arr_money[1].
hope this will sure help you,
You can use explode() reference PHP explode
$pointAndmoney = "356+2311";
list($point,$money) = explode('+',$pointAndmoney); // now you have different variable for each
$arr = explode("+","54+99");
$point = $arr[0];
$money = $arr[1];
using php. I have the following number
4,564,454
454,454,454
54.54
65.43
I want to convert these into number for calculating. How can I do it? Right now, the type of these number is string.
Note: the comma is not a separate of a number, it is a notion to make a number nicer. I got this format from the ajax request, I cant change the format though. So, I have to use it.
Thanks
$var = floatval(str_replace(",", "", "454,454,454"));
$a='4,5,4';
$ab= explode(',', $a);
foreach ($ab as $b)
{
$sum+=$b; //perform your calculation
}
echo $sum;
First you need to remove ,(comma) from your string as below :
$str=str_replace(",", "", "454,454,454");
Then converting in numeric:
$int = (int)$str;
or
$int=intval($str);
now do your calculation using $int variable.
try this code
$str = '4,564,454';
$str2 = '454,454,454';
$str3= '54.54';
$str4= '65.43';
$sum=0;
$sum += array_sum(explode(',',$str));
$sum += array_sum(explode(',',$str2));
$sum += $str3;
$sum += $str4;
echo $sum;
I have a whole bunch of percentages stored as XX% (e.g. 12%, 50%, etc..) I need to remove the percentage sign and then multiply the percent against another variable thats just a number (e.g. 1000, 12000) and then output the result. Is there a simple way to strip the percentage sign and then calculate the output with PHP? Or should I consider some sort of JS solution?
You could use rtrim():
$value = ((int) rtrim('12%', '%')) * 1000';
Edit
You don't strictly need to call rtrim() , as it casts to an int ok with the percentage sign. It is probably cleaner to strip it though.
var_dump (12 === (int) '12%');
//output: bool(true)
You can make use of preg_replace_callback as:
$input = '12%, 50%';
$input = preg_replace_callback("|(\d+)%|","replace_precent",$input);
echo $input; // 12000, 50000
function replace_precent($matches) {
return $matches[1] * 1000;
}
Try this:
$number = str_replace('%', '', '100%');
$result = intval($number) * 5000; // or whatever number
echo $result;
If you use trim() or str_replace() in PHP you can remove the percent sign. Then, you should be able to multiply the resulting number (php is weakly typed after all).
<?php
$number = str_replace("%", "", $percentString);
$newNumber = ((int) $number) * 1000;
echo $newNumber;
?>
You can use str_replace. You can also pass an array of subjects into str_replace to have them all replaced.
<?php
$number = str_replace("%", "", $percentage);
$result = $number * $other_var;
print $result;
?>
<?php
$input=array('15%','50%','10.99%','21.5%');
$multiplier=1000;
foreach($input as $n){
$z=floatval($n)*$multiplier;
print("$z<br>");
}
?>
Consider my variable $result to be 01212.... Now if i add 1 to my variable i get the answer 1213,but i want it to be 01213.... My php code is
echo sprintf('%02u', ($result+1));
Edit:
Answers to this question works.. But what happens if my variable is $result to be 0121212
in future...
You can use %05u instead on %02u
echo sprintf('%05u', ($result+1));
EDIT:
To generalize it:
<?php
$result = "0121211";
$len = strlen($result+1) + 1;
printf("%0${len}d", ($result+1)); // print 0121212
?>
you could try:
str_pad($result, 5, "0", STR_PAD_LEFT);
Maybe I'm missing something here but it could be as simple as
$result = '0'. ($result+1);
edit:
$test = array('01212', '0121212', '012121212121212', '-01212');
foreach( $test as $result ) {
$result = '0'.($result+1);
echo $result, "\n";
}
prints
01213
0121213
012121212121213
0-1211
( you see, there are limitations ;-) )
Read here: http://php.net/manual/en/function.sprintf.php
in sprintf, you also has to specify length you want - so in your case, if you want any number to be shown 5chars long, you haveto write
echo sprintf ('%05d', $d); // 5 places taking decimal
or even better
printf ('%05d', $d);
Seems like you want to have a '0' in front of the number, so here would be the simplest :P
echo '0'. $result;
Or if you insist on using sprintf:
$newresult = $result + 1;
echo sprintf('%0'.(strlen(strval($newresult))+1).'u', $newresult);
Just looked at function
str_pad($input, $pad_length, $pad_str, [STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH])
which helps to pad some string on left, right or on both sides of a given input.
Is there any php function which I can use to insert a string inside an input string?
for example ..
$input = "abcdef";
$pad_str = "#";
so if I give insert index 3, it inserts "#" after first 3 left most characters and $input becomes "abc#def".
thanks
You're looking for a string insert, not a padding.
Padding makes a string a set length, if it's not already at that length, so if you were to give a pad length 3 to "abcdef", well it's already at 3, so nothing should happen.
Try:
$newstring = substr_replace($orig_string, $insert_string, $position, 0);
PHP manual on substr_replace
you need:
substr($input, 0, 3).$pad_str.substr($input, 3)
Bah, I misread the question. You want a single insert, not insert every X characters. Sorry.
I'll leave it here so it's not wasted.
You can use regular expressions and some calculation to get your desired result (you probably could make it with pure regexp, but that would be more complex and less readable)
vinko#mithril:~$ more re.php
<?php
$test1 = "123123123";
$test2 = "12312";
echo puteveryXcharacters($a,"#",3);
echo "\n";
echo puteveryXcharacters($b,"#",3);
echo "\n";
echo puteveryXcharacters($b,"$",3);
echo "\n";
function puteveryXcharacters($str,$wha,$cnt) {
$strip = false;
if (strlen($str) % $cnt == 0) {
$strip = true;
}
$tmp = preg_replace('/(.{'.$cnt.'})/',"$1$wha", $str);
if ($strip) {
$tmp = substr($tmp,0,-1);
}
return $tmp;
}
?>
vinko#mithril:~$ php re.php
123#123#123
123#12
123$12