What's wrong with my implementation of HMAC in PHP? - php

I'm trying to implement a generic HMAC function in PHP. I'm following the RFC and the wikipedia page, but I cannot get my function to match the sample outputs. What am I doing wrong?
function myHmac(string $data, string $key, callable $algoFn, int $algoBlockSizeInBytes): string
{
if (strlen($key) > $algoBlockSizeInBytes)
{
$key = $algoFn($key); // keys longer than blocksize are shortened
}
if (strlen($key) < $algoBlockSizeInBytes)
{
$key = $key . str_repeat(0x00, $algoBlockSizeInBytes - strlen($key)); // keys shorter than blocksize are zero-padded
}
$outerKeyPad = str_repeat(0x5c, $algoBlockSizeInBytes) ^ $key;
$innerKeyPad = str_repeat(0x36, $algoBlockSizeInBytes) ^ $key;
return bin2hex($algoFn($outerKeyPad . $algoFn($innerKeyPad . $data)));
}
$md5Fn = function ($str) { return md5($str, true); };
echo 'my output: ' . myHmac("", "", $md5Fn, 64) . "\n";
echo 'correct output: ' . hash_hmac('md5', "", "") . "\n";

You invoke string_repeat() with an integer instead of the string to be repeated. So integer to string conversion applies. This means that currently you get something like 0x36 -> '54' -> '54545454...5454' ('54' repeated 64 times) and so on. Use chr(0x36) to get the actual byte.

Related

PHP Remove Unnecessary Zero In Decimals Works, But Not With This Number [duplicate]

I'm trying to find a fast way to remove zero decimals from number values like this:
echo cleanNumber('125.00');
// 125
echo cleanNumber('966.70');
// 966.7
echo cleanNumber(844.011);
// 844.011
Does exists some optimized way to do that?
$num + 0 does the trick.
echo 125.00 + 0; // 125
echo '125.00' + 0; // 125
echo 966.70 + 0; // 966.7
Internally, this is equivalent to casting to float with (float)$num or floatval($num) but I find it simpler.
you could just use the floatval function
echo floatval('125.00');
// 125
echo floatval('966.70');
// 966.7
echo floatval('844.011');
// 844.011
This is what I use:
function TrimTrailingZeroes($nbr) {
return strpos($nbr,'.')!==false ? rtrim(rtrim($nbr,'0'),'.') : $nbr;
}
N.B. This assumes . is the decimal separator. It has the advantage that it will work on arbitrarily large (or small) numbers since there is no float cast. It also won't turn numbers into scientific notation (e.g. 1.0E-17).
If you want to remove the zero digits just before to display on the page or template.
You can use the sprintf() function
sprintf('%g','125.00');
// 125
‌‌sprintf('%g','966.70');
// 966.7
‌‌‌‌sprintf('%g',844.011);
// 844.011
Simply adding + to your string variable will cause typecast to (float) and removes zeros:
var_dump(+'125.00'); // double(125)
var_dump(+'966.70'); // double(966.7)
var_dump(+'844.011'); // double(844.011)
var_dump(+'844.011asdf');// double(844.011)
For everyone coming to this site having the same problem with commata instead, change:
$num = number_format($value, 1, ',', '');
to:
$num = str_replace(',0', '', number_format($value, 1, ',', '')); // e.g. 100,0 becomes 100
If there are two zeros to be removed, then change to:
$num = str_replace(',00', '', number_format($value, 2, ',', '')); // e.g. 100,00 becomes 100
More here: PHP number: decimal point visible only if needed
You should cast your numbers as floats, which will do this for you.
$string = "42.422005000000000000000000000000";
echo (float)$string;
Output of this will be what you are looking for.
42.422005
$x = '100.10';
$x = preg_replace("/\.?0*$/",'',$x);
echo $x;
There is nothing that can't be fixed with a simple regex ;)
http://xkcd.com/208/
Typecast to a float.
$int = 4.324000;
$int = (float) $int;
Be careful with adding +0.
echo number_format(1500.00, 2,".",",")+0;
//1
Result of this is 1.
echo floatval('1,000.00');
// 1
echo floatval('1000.00');
//1000
Sometimes, especially in case of monetary amounts, you want to remove the zeros only if they are 2, you don't want to print € 2.1 instead of € 2.10.
An implementation could be:
function formatAmount(string|float|int $value, int $decimals = 2): string
{
if (floatval(intval($value)) === floatval($value)) {
// The number is an integer. Remove all the decimals
return (string)intval($value);
}
return number_format($value, $decimals);
}
Examples of expected outputs:
0.1000 => 0.10
20.000 => 20
1.25 => 1.25
Example 1
$value =81,500.00;
{{rtrim(rtrim(number_format($value,2),0),'.')}}
output
81,500
Example 2
$value=110,763.14;
{{rtrim(rtrim(number_format($value,2),0),'.')}}
output
110,763.14
Due to this question is old. First, I'm sorry about this.
The question is about number xxx.xx but in case that it is x,xxx.xxxxx or difference decimal separator such as xxxx,xxxx this can be harder to find and remove zero digits from decimal value.
/**
* Remove zero digits (include zero trails - 123.450, 123.000) from decimal value.
*
* #param string|int|float $number The number can be any format, any where use in the world such as 123, 1,234.56, 1234.56789, 12.345,67, -98,765.43
* #param string The decimal separator. You have to set this parameter to exactly what it is. For example: in Europe it is mostly use "," instead of ".".
* #return string Return removed zero digits from decimal value. Only return as string!
*/
function removeZeroDigitsFromDecimal($number, $decimal_sep = '.')
{
$explode_num = explode($decimal_sep, $number);
if (is_countable($explode_num) && count($explode_num) > 1) {
// if exploded number is more than 1 (Example: explode with . for nnnn.nnn is 2)
// replace `is_countable()` with `is_array()` if you are using PHP older than 7.3.
$explode_num[count($explode_num)-1] = preg_replace('/(0+)$/', '', $explode_num[count($explode_num)-1]);
if ($explode_num[count($explode_num)-1] === '') {
// if the decimal value is now empty.
// unset it to prevent nnn. without any number.
unset($explode_num[count($explode_num)-1]);
}
$number = implode($decimal_sep, $explode_num);
}
unset($explode_num);
return (string) $number;
}
And here is the code for test.
$tests = [
1234 => 1234,
-1234 => -1234,
'12,345.67890' => '12,345.6789',
'-12,345,678.901234' => '-12,345,678.901234',
'12345.000000' => '12345',
'-12345.000000' => '-12345',
'12,345.000000' => '12,345',
'-12,345.000000000' => '-12,345',
];
foreach ($tests as $number => $assert) {
$result = removeZeroDigitsFromDecimal($number);
assert($result === (string) $assert, new \Exception($result . ' (' . gettype($result) . ') is not matched ' . $assert . ' (' . gettype($assert) . ')'));
echo $number . ' => ' . (string) $assert . '<br>';
}
echo '<hr>' . PHP_EOL;
$tests = [
1234 => 1234,
-1234 => -1234,
'12.345,67890' => '12.345,6789',
'-12.345.678,901234' => '-12.345.678,901234',
'12345,000000' => '12345',
'-12345,000000' => '-12345',
'-12.345,000000000' => '-12.345',
'-12.345,000000,000' => '-12.345,000000',// this is correct assertion. Weird ,000000,000 but only last 000 will be removed.
];
foreach ($tests as $number => $assert) {
$result = removeZeroDigitsFromDecimal($number, ',');
assert($result === (string) $assert, new \Exception($result . ' (' . gettype($result) . ') is not matched ' . $assert . ' (' . gettype($assert) . ')'));
echo $number . ' => ' . (string) $assert . '<br>';
}
All tests should be pass and no errors.
Why '-12.345,000000,000' will be '-12.345,000000' not '-12.345'?
Because this function is for remove zero digits (include zero trails) from decimal value. It is not validation for the correct number format. That should be another function.
Why always return as string?
Because it is better to use in calculation with bcxxx functions, or use with big number.
$str = 15.00;
$str2 = 14.70;
echo rtrim(rtrim(strval($str), "0"), "."); //15
echo rtrim(rtrim(strval($str2), "0"), "."); //14.7
I found this solution is the best:
public function priceFormat(float $price): string
{
//https://stackoverflow.com/a/14531760/5884988
$price = $price + 0;
$split = explode('.', $price);
return number_format($price, isset($split[1]) ? strlen($split[1]) : 2, ',', '.');
}
The following is much simpler
if(floor($num) == $num) {
echo number_format($num);
} else {
echo $num;
}
You can try the following:
rtrim(number_format($coin->current_price,6),'0.')
Complicated way but works:
$num = '125.0100';
$index = $num[strlen($num)-1];
$i = strlen($num)-1;
while($index == '0') {
if ($num[$i] == '0') {
$num[$i] = '';
$i--;
}
$index = $num[$i];
}
//remove dot if no numbers exist after dot
$explode = explode('.', $num);
if (isset($explode[1]) && intval($explode[1]) <= 0) {
$num = intval($explode[0]);
}
echo $num; //125.01
the solutions above are the optimal way but in case you want to have your own you could use this. What this algorithm does it starts at the end of string and checks if its 0, if it is it sets to empty string and then goes to the next character from back untill the last character is > 0
$value = preg_replace('~\.0+$~','',$value);
You can use:
print (floatval)(number_format( $Value), 2 ) );
Thats my small solution...
Can included to a class and set vars
private $dsepparator = '.'; // decimals
private $tsepparator= ','; // thousand
That can be set by constructor and change to users lang.
class foo
{
private $dsepparator;
private $tsepparator;
function __construct(){
$langDatas = ['en' => ['dsepparator' => '.', 'tsepparator' => ','], 'de' => ['dsepparator' => ',', 'tsepparator' => '.']];
$usersLang = 'de'; // set iso code of lang from user
$this->dsepparator = $langDatas[$usersLang]['dsepparator'];
$this->tsepparator = $langDatas[$usersLang]['tsepparator'];
}
public function numberOmat($amount, $decimals = 2, $hideByZero = false)
{
return ( $hideByZero === true AND ($amount-floor($amount)) <= 0 ) ? number_format($amount, 0, $this->dsepparator, $this->tsepparator) : number_format($amount, $decimals, $this->dsepparator, $this->tsepparator);
}
/*
* $bar = new foo();
* $bar->numberOmat('5.1234', 2, true); // returns: 5,12
* $bar->numberOmat('5', 2); // returns: 5,00
* $bar->numberOmat('5.00', 2, true); // returns: 5
*/
}
This is my solution.
I want to keep ability to add thousands separator
$precision = 5;
$number = round($number, $precision);
$decimals = strlen(substr(strrchr($number, '.'), 1));
return number_format($number, $precision, '.', ',');
This is a simple one line function using rtrim, save separator and decimal point :
function myFormat($num,$dec)
{
return rtrim(rtrim(number_format($num,$dec),'0'),'.');
}
Simple and accurate!
function cleanNumber($num){
$explode = explode('.', $num);
$count = strlen(rtrim($explode[1],'0'));
return bcmul("$num",'1', $count);
}
I use this simple code:
define('DECIMAL_SEPARATOR', ','); //To prove that it works with different separators than "."
$input = "50,00";
$number = rtrim($input, '0'); // 50,00 --> 50,
$number = rtrim($number, DECIMAL_SEPARATOR); // 50, --> 50
echo $number;
Seems a bit too easy to be the really correct solution, but it works just fine for me. You should do some tests with the inputs you'll be getting before using this.
Assuming the amount is a string with 2 decimal places, then you can use:
protected function removeZerosDecimals(string $money): string
{
$formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
$uselessDecimals = sprintf(
'%s00',
$formatter->getSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL)
);
if (!str_ends_with($money, $uselessDecimals)) {
return $money;
}
$len = mb_strlen($money);
return mb_substr($money, 0, $len - mb_strlen($uselessDecimals));
}
That will work as expected for ?any? currency, like $500.00 and R$ 500,00.
function removeZerosAfterDecimals($number) {
$number = trim($number);
if($number <= 0 || empty($number)) {
return $number;
}
$ary = explode('.', $number);
if(count($ary) <= 1) {
return $number;
}
$reverseAry = array_reverse($ary);
$endSearch = false;
$newNumber = [];
for($i=0; $i<count($reverseAry); $i++) {
if($reverseAry[$i] != 0 && $endSearch === false) {
$newNumber[] = $reverseAry[$i];
$endSearch = true;
} else if ($endSearch === true) {
$newNumber[] = $reverseAry[$i];
}
}
return implode('.',array_reverse($newNumber));
}
//output: 10.0.1.0 => 10.0.1
//output: 10.0.1 => 10.0.1
//output: 10.1.2 => 10.1.2
//output: 10.0.0 => 10
This function will only remove the trailing zero decimals
This Code will remove zero after point and will return only two decimal digits.
$number=1200.0000;
str_replace('.00', '',number_format($number, 2, '.', ''));
Output will be: 1200
Ultimate Solution: The only safe way is to use regex:
echo preg_replace("/\.?0+$/", "", 3.0); // 3
echo preg_replace("/\d+\.?\d*(\.?0+)/", "", 3.0); // 3
it will work for any case

Error in my encryption code in php

Please what could be wrong with my code? The code is meant to encrypt a given string. It does the encryption quite alright but when echoing the encrypted value, it adds numbers to the it. Here is the code:
$keys = "Bb94tU1LSSLgPKNu";
$encrypt = array();
// this contains the key/value pair i used for the encryption
$arrayEncrypt = array("a"=>"g","b"=>"h","c"=>"i","d"=>"j","e"=>"k","f"=>"l","g"=>"m","h"=>"n","i"=>"o","j"=>"p","k"=>"q","l"=>"r", "m"=>"s","n"=>"t","o"=>"u","p"=>"v","q"=>"w","r"=>"x","s"=>"y","t"=>"z","u"=>"a","v"=>"b","w"=>"c","x"=>"d","y"=>"e","z"=>"f","A"=>"G","B"=>"H","C"=>"I","D"=>"J","E"=>"K","F"=>"L","G"=>"M","H"=>"N","I"=>"O","J"=>"P","K"=>"Q","L"=>"R","M"=>"S","N"=>"T","O"=>"U","P"=>"V","Q"=>"W","R"=>"X","S"=>"Y","T"=>"Z","U"=>"A","V"=>"B","W"=>"C","X"=>"D","Y"=>"E","Z"=>"F","0"=>"2","1"=>"4","2"=>"6","3"=>"8","4"=>"0","5"=>"9","6"=>"7","7"=>"5","8"=>"3","9"=>"1");
for ($i = 0; $i < strlen($keys); $i++) {
if (array_key_exists($keys[$i], $arrayEncrypt)) {
foreach ($arrayEncrypt as $key => $letter) {
if ($keys[$i] == $key) {
// appends the values of each in $encrypt array
array_push($encrypt, $letter);
}
}
}
}
$encryptedValue = "";
foreach ($encrypt as $encrypted) {
$encryptedValue .= $encrypted;
echo $encryptedValue;
}
It prints:
H2h210z2A24R2Y2Y2R2m2V2Q2T2a2
Instead of:
Hh10zA4RYYRmVQTa
In any case, you were over complicating it trying to use the $i instead of just using the name to get the matching value. str_split() to get an array of characters then it's just a matter of $arrayEncrypt[$value]. To undo you can use array_flip().
Made you a nice function to encrypt and decrypt. Enjoy
<?php
// 1 to encrypt
// 2 to decrypt
function encdec($do, $keys){
$arrayEncrypt = array("a"=>"g","b"=>"h","c"=>"i","d"=>"j","e"=>"k","f"=>"l","g"=>"m","h"=>"n","i"=>"o","j"=>"p","k"=>"q","l"=>"r", "m"=>"s","n"=>"t","o"=>"u","p"=>"v","q"=>"w","r"=>"x","s"=>"y","t"=>"z","u"=>"a","v"=>"b","w"=>"c","x"=>"d","y"=>"e","z"=>"f","A"=>"G","B"=>"H","C"=>"I","D"=>"J","E"=>"K","F"=>"L","G"=>"M","H"=>"N","I"=>"O","J"=>"P","K"=>"Q","L"=>"R","M"=>"S","N"=>"T","O"=>"U","P"=>"V","Q"=>"W","R"=>"X","S"=>"Y","T"=>"Z","U"=>"A","V"=>"B","W"=>"C","X"=>"D","Y"=>"E","Z"=>"F","0"=>"2","1"=>"4","2"=>"6","3"=>"8","4"=>"0","5"=>"9","6"=>"7","7"=>"5","8"=>"3","9"=>"1");
if ($do==2){
$arrayEncrypt=array_flip($arrayEncrypt);
}
$chars = str_split($keys);
$encryptedValue="";
foreach($chars as $char=>$value){
$encryptedValue .= $arrayEncrypt[$value];
}
return $encryptedValue;
}
echo "Encrypted key: ".encdec(1,"Bb94tU1LSSLgPKNu")."<br />";
echo "Decrypted key: ".encdec(2,"Hh10zA4RYYRmVQTa")."<br />";
?>

Php change a number into another number that can be changed back to the original

Using PHP, I'm trying to encode a number into another number that I can decode back to the original number. The encoded string needs to be only numbers and should not contain anything else.
Eg: 10 becomes 573563547892 or something like that.
How can I do something like this in PHP? I tried quite a few encrypt decrypt functions, but none output only numbers.
I looking for something to use in a URL that isn't easy to guess.
So: http://www.me.com/index.PHP?page=20 becomes http://www.me.com/index.PHP?page=5705254782562466
Why not using a mathematicat operation on the original number? like x becomes x * y + z. you would only have to make the reverse operation to get the original number. consider using large enough prime numbers for y and/or z
Quite heavy, but very good encryption, by using ord & chr a bit. While this works, consider other options: just being able to use strings rather then numbers already makes it a lot simpler (base64_encode etc.):
<?php
class Crypter {
private $key = '';
private $iv = '';
function __construct($key,$iv){
$this->key = $key;
$this->iv = $iv;
}
protected function getCipher(){
$cipher = mcrypt_module_open(MCRYPT_BLOWFISH,'','cbc','');
mcrypt_generic_init($cipher, $this->key, $this->iv);
return $cipher;
}
function encrypt($string){
$binary = mcrypt_generic($this->getCipher(),$string);
$string = '';
for($i = 0; $i < strlen($binary); $i++){
$string .= str_pad(ord($binary[$i]),3,'0',STR_PAD_LEFT);
}
return $string;
}
function decrypt($encrypted){
//check for missing leading 0's
$encrypted = str_pad($encrypted, ceil(strlen($encrypted) / 3) * 3,'0', STR_PAD_LEFT);
$binary = '';
$values = str_split($encrypted,3);
foreach($values as $chr){
$chr = ltrim($chr,'0');
$binary .= chr($chr);
}
return mdecrypt_generic($this->getCipher(),$binary);
}
}
$crypt = new Crypter('secret key','12348765');
$encrypted = $crypt->encrypt(1234);
echo $encrypted.PHP_EOL;
//fake missing leading 0
$encrypted = ltrim($encrypted,'0');
echo $encrypted.PHP_EOL;
$decrypted = $crypt->decrypt($encrypted);
echo $decrypted.PHP_EOL;
Result:
057044206104214236155088
57044206104214236155088
1234

Remove useless zero digits from decimals in PHP

I'm trying to find a fast way to remove zero decimals from number values like this:
echo cleanNumber('125.00');
// 125
echo cleanNumber('966.70');
// 966.7
echo cleanNumber(844.011);
// 844.011
Does exists some optimized way to do that?
$num + 0 does the trick.
echo 125.00 + 0; // 125
echo '125.00' + 0; // 125
echo 966.70 + 0; // 966.7
Internally, this is equivalent to casting to float with (float)$num or floatval($num) but I find it simpler.
you could just use the floatval function
echo floatval('125.00');
// 125
echo floatval('966.70');
// 966.7
echo floatval('844.011');
// 844.011
This is what I use:
function TrimTrailingZeroes($nbr) {
return strpos($nbr,'.')!==false ? rtrim(rtrim($nbr,'0'),'.') : $nbr;
}
N.B. This assumes . is the decimal separator. It has the advantage that it will work on arbitrarily large (or small) numbers since there is no float cast. It also won't turn numbers into scientific notation (e.g. 1.0E-17).
If you want to remove the zero digits just before to display on the page or template.
You can use the sprintf() function
sprintf('%g','125.00');
// 125
‌‌sprintf('%g','966.70');
// 966.7
‌‌‌‌sprintf('%g',844.011);
// 844.011
Simply adding + to your string variable will cause typecast to (float) and removes zeros:
var_dump(+'125.00'); // double(125)
var_dump(+'966.70'); // double(966.7)
var_dump(+'844.011'); // double(844.011)
var_dump(+'844.011asdf');// double(844.011)
For everyone coming to this site having the same problem with commata instead, change:
$num = number_format($value, 1, ',', '');
to:
$num = str_replace(',0', '', number_format($value, 1, ',', '')); // e.g. 100,0 becomes 100
If there are two zeros to be removed, then change to:
$num = str_replace(',00', '', number_format($value, 2, ',', '')); // e.g. 100,00 becomes 100
More here: PHP number: decimal point visible only if needed
You should cast your numbers as floats, which will do this for you.
$string = "42.422005000000000000000000000000";
echo (float)$string;
Output of this will be what you are looking for.
42.422005
$x = '100.10';
$x = preg_replace("/\.?0*$/",'',$x);
echo $x;
There is nothing that can't be fixed with a simple regex ;)
http://xkcd.com/208/
Typecast to a float.
$int = 4.324000;
$int = (float) $int;
Be careful with adding +0.
echo number_format(1500.00, 2,".",",")+0;
//1
Result of this is 1.
echo floatval('1,000.00');
// 1
echo floatval('1000.00');
//1000
Sometimes, especially in case of monetary amounts, you want to remove the zeros only if they are 2, you don't want to print € 2.1 instead of € 2.10.
An implementation could be:
function formatAmount(string|float|int $value, int $decimals = 2): string
{
if (floatval(intval($value)) === floatval($value)) {
// The number is an integer. Remove all the decimals
return (string)intval($value);
}
return number_format($value, $decimals);
}
Examples of expected outputs:
0.1000 => 0.10
20.000 => 20
1.25 => 1.25
Example 1
$value =81,500.00;
{{rtrim(rtrim(number_format($value,2),0),'.')}}
output
81,500
Example 2
$value=110,763.14;
{{rtrim(rtrim(number_format($value,2),0),'.')}}
output
110,763.14
Due to this question is old. First, I'm sorry about this.
The question is about number xxx.xx but in case that it is x,xxx.xxxxx or difference decimal separator such as xxxx,xxxx this can be harder to find and remove zero digits from decimal value.
/**
* Remove zero digits (include zero trails - 123.450, 123.000) from decimal value.
*
* #param string|int|float $number The number can be any format, any where use in the world such as 123, 1,234.56, 1234.56789, 12.345,67, -98,765.43
* #param string The decimal separator. You have to set this parameter to exactly what it is. For example: in Europe it is mostly use "," instead of ".".
* #return string Return removed zero digits from decimal value. Only return as string!
*/
function removeZeroDigitsFromDecimal($number, $decimal_sep = '.')
{
$explode_num = explode($decimal_sep, $number);
if (is_countable($explode_num) && count($explode_num) > 1) {
// if exploded number is more than 1 (Example: explode with . for nnnn.nnn is 2)
// replace `is_countable()` with `is_array()` if you are using PHP older than 7.3.
$explode_num[count($explode_num)-1] = preg_replace('/(0+)$/', '', $explode_num[count($explode_num)-1]);
if ($explode_num[count($explode_num)-1] === '') {
// if the decimal value is now empty.
// unset it to prevent nnn. without any number.
unset($explode_num[count($explode_num)-1]);
}
$number = implode($decimal_sep, $explode_num);
}
unset($explode_num);
return (string) $number;
}
And here is the code for test.
$tests = [
1234 => 1234,
-1234 => -1234,
'12,345.67890' => '12,345.6789',
'-12,345,678.901234' => '-12,345,678.901234',
'12345.000000' => '12345',
'-12345.000000' => '-12345',
'12,345.000000' => '12,345',
'-12,345.000000000' => '-12,345',
];
foreach ($tests as $number => $assert) {
$result = removeZeroDigitsFromDecimal($number);
assert($result === (string) $assert, new \Exception($result . ' (' . gettype($result) . ') is not matched ' . $assert . ' (' . gettype($assert) . ')'));
echo $number . ' => ' . (string) $assert . '<br>';
}
echo '<hr>' . PHP_EOL;
$tests = [
1234 => 1234,
-1234 => -1234,
'12.345,67890' => '12.345,6789',
'-12.345.678,901234' => '-12.345.678,901234',
'12345,000000' => '12345',
'-12345,000000' => '-12345',
'-12.345,000000000' => '-12.345',
'-12.345,000000,000' => '-12.345,000000',// this is correct assertion. Weird ,000000,000 but only last 000 will be removed.
];
foreach ($tests as $number => $assert) {
$result = removeZeroDigitsFromDecimal($number, ',');
assert($result === (string) $assert, new \Exception($result . ' (' . gettype($result) . ') is not matched ' . $assert . ' (' . gettype($assert) . ')'));
echo $number . ' => ' . (string) $assert . '<br>';
}
All tests should be pass and no errors.
Why '-12.345,000000,000' will be '-12.345,000000' not '-12.345'?
Because this function is for remove zero digits (include zero trails) from decimal value. It is not validation for the correct number format. That should be another function.
Why always return as string?
Because it is better to use in calculation with bcxxx functions, or use with big number.
$str = 15.00;
$str2 = 14.70;
echo rtrim(rtrim(strval($str), "0"), "."); //15
echo rtrim(rtrim(strval($str2), "0"), "."); //14.7
I found this solution is the best:
public function priceFormat(float $price): string
{
//https://stackoverflow.com/a/14531760/5884988
$price = $price + 0;
$split = explode('.', $price);
return number_format($price, isset($split[1]) ? strlen($split[1]) : 2, ',', '.');
}
The following is much simpler
if(floor($num) == $num) {
echo number_format($num);
} else {
echo $num;
}
You can try the following:
rtrim(number_format($coin->current_price,6),'0.')
Complicated way but works:
$num = '125.0100';
$index = $num[strlen($num)-1];
$i = strlen($num)-1;
while($index == '0') {
if ($num[$i] == '0') {
$num[$i] = '';
$i--;
}
$index = $num[$i];
}
//remove dot if no numbers exist after dot
$explode = explode('.', $num);
if (isset($explode[1]) && intval($explode[1]) <= 0) {
$num = intval($explode[0]);
}
echo $num; //125.01
the solutions above are the optimal way but in case you want to have your own you could use this. What this algorithm does it starts at the end of string and checks if its 0, if it is it sets to empty string and then goes to the next character from back untill the last character is > 0
$value = preg_replace('~\.0+$~','',$value);
You can use:
print (floatval)(number_format( $Value), 2 ) );
Thats my small solution...
Can included to a class and set vars
private $dsepparator = '.'; // decimals
private $tsepparator= ','; // thousand
That can be set by constructor and change to users lang.
class foo
{
private $dsepparator;
private $tsepparator;
function __construct(){
$langDatas = ['en' => ['dsepparator' => '.', 'tsepparator' => ','], 'de' => ['dsepparator' => ',', 'tsepparator' => '.']];
$usersLang = 'de'; // set iso code of lang from user
$this->dsepparator = $langDatas[$usersLang]['dsepparator'];
$this->tsepparator = $langDatas[$usersLang]['tsepparator'];
}
public function numberOmat($amount, $decimals = 2, $hideByZero = false)
{
return ( $hideByZero === true AND ($amount-floor($amount)) <= 0 ) ? number_format($amount, 0, $this->dsepparator, $this->tsepparator) : number_format($amount, $decimals, $this->dsepparator, $this->tsepparator);
}
/*
* $bar = new foo();
* $bar->numberOmat('5.1234', 2, true); // returns: 5,12
* $bar->numberOmat('5', 2); // returns: 5,00
* $bar->numberOmat('5.00', 2, true); // returns: 5
*/
}
This is my solution.
I want to keep ability to add thousands separator
$precision = 5;
$number = round($number, $precision);
$decimals = strlen(substr(strrchr($number, '.'), 1));
return number_format($number, $precision, '.', ',');
This is a simple one line function using rtrim, save separator and decimal point :
function myFormat($num,$dec)
{
return rtrim(rtrim(number_format($num,$dec),'0'),'.');
}
Simple and accurate!
function cleanNumber($num){
$explode = explode('.', $num);
$count = strlen(rtrim($explode[1],'0'));
return bcmul("$num",'1', $count);
}
I use this simple code:
define('DECIMAL_SEPARATOR', ','); //To prove that it works with different separators than "."
$input = "50,00";
$number = rtrim($input, '0'); // 50,00 --> 50,
$number = rtrim($number, DECIMAL_SEPARATOR); // 50, --> 50
echo $number;
Seems a bit too easy to be the really correct solution, but it works just fine for me. You should do some tests with the inputs you'll be getting before using this.
Assuming the amount is a string with 2 decimal places, then you can use:
protected function removeZerosDecimals(string $money): string
{
$formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
$uselessDecimals = sprintf(
'%s00',
$formatter->getSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL)
);
if (!str_ends_with($money, $uselessDecimals)) {
return $money;
}
$len = mb_strlen($money);
return mb_substr($money, 0, $len - mb_strlen($uselessDecimals));
}
That will work as expected for ?any? currency, like $500.00 and R$ 500,00.
function removeZerosAfterDecimals($number) {
$number = trim($number);
if($number <= 0 || empty($number)) {
return $number;
}
$ary = explode('.', $number);
if(count($ary) <= 1) {
return $number;
}
$reverseAry = array_reverse($ary);
$endSearch = false;
$newNumber = [];
for($i=0; $i<count($reverseAry); $i++) {
if($reverseAry[$i] != 0 && $endSearch === false) {
$newNumber[] = $reverseAry[$i];
$endSearch = true;
} else if ($endSearch === true) {
$newNumber[] = $reverseAry[$i];
}
}
return implode('.',array_reverse($newNumber));
}
//output: 10.0.1.0 => 10.0.1
//output: 10.0.1 => 10.0.1
//output: 10.1.2 => 10.1.2
//output: 10.0.0 => 10
This function will only remove the trailing zero decimals
This Code will remove zero after point and will return only two decimal digits.
$number=1200.0000;
str_replace('.00', '',number_format($number, 2, '.', ''));
Output will be: 1200
Ultimate Solution: The only safe way is to use regex:
echo preg_replace("/\.?0+$/", "", 3.0); // 3
echo preg_replace("/\d+\.?\d*(\.?0+)/", "", 3.0); // 3
it will work for any case

Little issue with escaping a whole string

I have the following code that does a XOR encode/decode procedure:
<?php
/*
* Simple XOR encoder/decoder, Inkubus
*
*/
$key = pack("H*","3cb37efae7f4f376ebbd76cd");
//$encoded = ")5/Y+F8'0P$/;"; // <- OK, Working
//$decoded = 'hM5cf$350';
$encoded = "-?^<]TLV.GQV)B4[YQ "; // <- BAD, Not working
$decoded = "CTC(*zlkb4848";
//$encoded = ')3\8?E(21#$;='; // <- OK, Working
//$decoded = 'suances06';
function decode($encoded,$key) {
$cipher = convert_uudecode($encoded);
$plain = $cipher ^ $key;
$result = substr($plain, 0, strlen($cipher));
return $result;
}
function encode($decoded,$key) {
$plain = $decoded ^ $key;
$result = convert_uuencode(substr($plain, 0, strlen($decoded)));
$result = preg_replace("/\n/m","",$result);
$result = preg_replace("/`$/m","",$result);
return $result;
}
echo "Encoded: " . $encoded . "\n";
echo "Decoded: " . decode($encoded,$key) . "\n";
echo "Encoded: " . encode($decoded,$key) . "\n";
echo "Decoded: " . decode(encode($decoded,$key),$key) . "\n";
?>
commented right after the header are two examples that work, the uncommented is the one with problem. How can I GENERICALLY comment the UUENCODED string to be decoded so it is kept unchanged and decoded right? Commenting any problematic character in the string is not an option, but the entire string as a whole.
Working run example:
bash-$ php xor.php
Encoded: )3\8?E(21#$;=
Decoded: suances06
Encoded: )3\8?E(21#$;=
Decoded: suances06
Not working example:
bash-$ php xor.php
Encoded: -?^<]TLV.GQV)B4[YQ
Decoded: CTC(*zlkb484
Encoded: ,?^<]TLV.GQV)B4[Y
Decoded: CTC(*zlkb484
Some character is lost or whatever. Any ideas?
Thanks!
UPDATE: Another not working example:
$encoded = "-!8H<RY67FP';C1+]R# "; // <- BAD, Not working
$decoded = "99b1rchw00d06";
Ran:
bash-$ php xor.php
Encoded: -!8H<RY67FP';C1+]R#
Decoded: 99b1rchw00d0
Encoded: ,!8H<RY67FP';C1+]
Decoded: 99b1rchw00d0
It's a unclear bit what you're actually asking here. Your functions contain a lot of cruft code and can/should be reduced to:
function decode($encoded,$key) {
return convert_uudecode($encoded) ^ $key;
}
function encode($decoded,$key) {
return convert_uuencode($decoded ^ $key);
}
The reason this does not work as you expect is because PHP's XOR doess not work the way you're hoping:
$a = "-?^<]TLV.GQV)B4[YQ ";
$b = pack("H*","3cb37efae7f4f376ebbd76cd");
$c = ($a ^ $b) ^ $b;
echo $a == $c ? 'OK' : 'NOT OK';
This will output NOT OK. If you want a simple string XOR operator, use this:
function str_xor($a, $b){
$out = '';
for ($i=0; $i<strlen($a); $i++){
$ac = ord($a{$i});
$bc = ord($b{$i});
$out .= chr($ac ^ $bc);
}
return $out;
}
The preceding example code will then round-trip:
$c = str_xor(str_xor($a, $b), $b);
echo $a == $c ? 'OK' : 'NOT OK';
# outputs 'OK'
PHP's XOR function does not work on strings where the 2nd operand is shorter that the first - it will truncate the returned string to the length of the 2nd operand (it's fine if the 2nd operand is longer than the first - it truncates the output to the length of the shortest operand).
Another possible solution is to just expand your key like this:
while (strlen($key) < strlen($text)) $key .= $key;
This will make sure that your key is longer than the thing you're XOR'ing

Categories