How to calculate currency string [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to calculate two variables type "String" in php.
and I try all alternative but steady amount missing values.
example:
$a = "250,000.50";
$b = "30,000.00";
echo $a - $b;
I want to return value with same format ###,###,###.## -String -Decimal -only return same format.
Note: I try all the link format currency here...use regex, sprintf, replace, etc...
but always lose a digit because the example use in format en_US or EUR.
Thanks!.

You can use this function for easily handle:
$a = "250,000.50";
$b = "30,000.00";
function currency($c=0){
return preg_replace("/[^\d\.]+/iu","",$c);
}
echo number_format(currency($a) - currency($b) ,2);

The subtraction on strings will auto-cast the strings into numbers for you, but you have to get rid of the commas first:
$a = "250,000.50";
$b = "30,000.00";
echo str_replace(',', '', $a) - str_replace(',', '', $b);
Oh, and wrap the whole thing in number_format to get it back to the comma format:
echo number_format( str_replace(',', '', $a) - str_replace(',', '', $b), 2);

Related

rearrange positive and negative numbers php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
An array has negative and positive numbers. Separate the numbers such that negative numbers are at the beginning and positive numbers at the end without changing the order.
Example:
Array = {1, -3, -5, 9 , -8}
O/P = {-3, -5, -8, 1, 9}
I found many answer in c , c++ , java but not in PHP , so can any one please let me know how or better way to achieve this?
However this question put on hold , i tried with some of solutions and
find my own answer added below , hope it might be useful for someone.
function part($arr){
$j = 0;
for($i=0;$i<count($arr);$i++){
$val = $arr[$i];
$k = $i;
while($k>$j && $val < 0){
$arr[$k] = $arr[$k-1];
$k = $k-1;
if($j==$k){
$j=$j+1;
}
$arr[$k] = $val;
}
}
return $arr;
}
$arr = array(1, -3, -5, 9 , -8);
print_r(part($arr));
Without doing all the work for you.
Look into array_filter you could filter the negative values into 1 array, then filter the positive values into another array.
Then use array_merge to merge the 2.

Maximum Explode variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I Want to explode custom numbers of variables.
My php code is here
$date="23-05-2015";
$var = "$var1, $var2, $var3";
// I want get $var value in list($var) = what I want{ list($var1, $var2, $var3)}
list($var) = explode('-', $date, 3);
The $var variable is weird, it keeps a list of comma-separated names of other variables....
Why would you do that ?
You have 2 options:
Either you use the list() on the variable names:
$date="23-05-2015";
list($var1, $var2, $var3) = explode('-', $date, 3);
Or you do the extract() hack, and keep the $var variable as is:
$date="23-05-2015";
$var = "$var1,$var2,$var3";
extract(array_combine(explode(',',$var,3),explode('-', $date, 3)))
Obviously, I think option #1 is the way to go

php string with all space changing [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have string like this : hello world new foo.
I need to have array like this:
[
`helloworld new foo`,
`hello worldnew foo`,
`hello world newfoo`,
`helloworldnew foo`,
`hello worldnewfoo`,
]
order doesn't matter but i need to have all case that space will be remove in current string.
You can use recursion to enumerate all the posibilities:
<?php
function combine($words, $acc=array(), $res=array()){
if(count($words)===0){
$res[] = join("", $acc);
return $res;
}
if(count($words)===1){
$res[] = join("", $acc).$words[0];
return $res;
}
$w1 = array_shift($words);
$res = combine($words, array_merge($acc, array("$w1")), $res);
$res = combine($words, array_merge($acc, array("$w1 ")), $res);
return $res;
}
var_dump( combine( explode(' ', 'hello world new foo') ) );
Another possible solution is to represent the N spaces betweeen your words, as bits in a binary number that can be on or off, and then count from 0 to 2^N-1, but I think that in PHP that will be more complex.
NB: the above recursive solution, returns all possible combinations ... so if you have an input array with 4 words, one of the results will have all 4 words joined.
From what i understood you need all posible combinations with the words given. so:
posible combinations = amountofwords*amountofspaces.
start iteration for amout of posible cominations. -> for(i=0;i<=(words*spaces);i++)
have words in array and spaces found in String so $WordArray = $string.split(" ") and $spaces = substr_count(" ")
start iteration for posible word combinations. for(j=0;j<=words;j++)
start iteration for amount of spaces. for(k=0;k<=spaces;++)
combine all.
but keep in mind that PERMUTATIONS and COMBINATIONS for computer science is what you need to learn first, so the answer above has a lot of sence.
here is a link to get you started.
http://cpsc.ualr.edu/srini/DM/chapters/review3.4.html

Regular expression php for a pro [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Can you help me with a regular expression for this:
$value = '["number"]';
or
$value = '["number","number"]';
or
$value = '["number","number","number"]';
or
...
...
...
...
$value = '["number","number","number","number","number","number","number"......,"number"]';
//$number can be rand(0, 99.....999);
I need the result to be validated as true or false!
Here is my version:
function test($v)
{
if (preg_match('/^\\[("number")(,"number")*\\]$/', $v))
echo 'ok<br>';
else
echo 'fail<br>';
}
or if "number" is really digits, this one:
function test($v)
{
if (preg_match('/^\\[("[0-9]+")(,"[0-9]+")*\\]$/', $v))
echo 'ok<br>';
else
echo 'fail<br>';
}
NOTE - only positive naturals are accepted, need to change to negative and decimal/floating numbers
Do you really need to use PCRE? Your example is valid json.
$array = json_decode('["number", "number", "123"]');
var_dump($array);
If you need numbers only, you can filter it.
$new_array = array_filter($array, 'ctype_digit');
$result = count($array) == $new_array? $new_array : null;
var_dump($result);

Array and string, exchange [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My code:
<?php
$string = "hello world";
echo $string[1]; //output is 'e'
echo end($string); // I've got a warning error
?>
How can I exchange my string variable to an array format?
I think if I exchange it the problem will solve.
$string[n] is a specific notation to access the nth offset of a string. This does not mean the string is an array, it's just special syntactic sugar. If you want the last offset of the string, use substr($string, -1).
There is a predefined function that do this. It's str_split(). Here http://it.php.net/manual/en/function.str-split.php you find the doc
Just do this:
echo substr($string, -1);
Because a string is not natively an array, functions like end and array_sort don't work.
To physically cast the string, use this:
function CastStringToArray($string)
{
$ret = array();
$length = strlen($string);
for ($i = 0; $i < $length; $i++)
{
$ret[] = $string[$i];
}
return $ret;
}

Categories