I'm creating player panel for my friend in php, but I have problem with variable shown below. I must insert into variable in php, and send request to database. What is my problem? I can't convert java code, because I don't know how I can do this. It may be strange, but unfortunately it is.
I tried do this with amateur way, using;
require 'mojang-api.class.php';
$uuid = MojangAPI::getUuid('jeb_');
echo 'UUID: <b>' . $uuid . '</b><br>';
echo substr($uuid, 0, 8); echo '-'; echo substr($uuid, 8, 12); echo '-'; substr($uuid, 13, 15); echo '-';
but, You and I know this - this way sucks.
I place the java code below.
Java:
uuid.substring(0, 8) + '-' + uuid.substring(8, 12) + '-' + uuid.substring(12, 16) + '-' + uuid.substring(16, 20) + '-' + uuid.substring(20)
If someone can help me with this problem, I'll be grateful.
Is this what you are looking for
$uuid = MojangAPI::getUuid('jeb_');
echo 'UUID: <b>' . $uuid . '</b><br>';
echo substr($uuid, 0, 8).'-'.substr($uuid, 8, 4).'-'.substr($uuid, 13, 2);
Instead of plus + in Java you use the dot . in PHP. Amazingly you used it in the line above?
Related
I'm trying to generate a number from 0 to 36 in php that can be proven that it was fair later.
I've had a google around and found a formula that works, however I'm having a bit of trouble getting it to work in PHP.
roll_hash = hash("sha256", server_seed + "-" + lottery + "-" + roll_id);
roll_hash_first16 = substr(roll_hash, 0, 16);
roll_lucky_number = hex_to_uint64(roll_hash_first16) % 15;
is the formula I've found, however I can't get it to work in PHP.
This is the PHP code I've tried however it doesn't do what I need it to do.
$server_seed = "577701c29f6e4a409b8a607cb95c79c943146dc7dff3eb6894a34837076e7365";
$salt = "fe5e5c41b";
$roll_id = 0;
$roll_hash = hash("sha256", $server_seed + "-" + $salt + "-" + $roll_id);
$roll_hash_first16 = substr($roll_hash, 0, 36);
$roll_lucky_number = hexdec($roll_hash_first16) / 35;
This code will just generate something like "5.3687216943283E+41"
I need it to generate a plain number from 0 to 36 and which should be the same whenever the server seed, salt and roll_id are the same.
Here's the corrected version of the PHP code though:
function generateRandomNumber(string $seed, string $salt, int $roll): int
{
$roll_hash = hash("sha256", $seed . "-" . $salt . "-" . $roll);
$roll_hash_first16 = substr($roll_hash, 0, 16);
return abs(hexdec($roll_hash_first16) % 37);
}
https://3v4l.org/QMgNR
I am trying to list the current USD price of an item in mBTC (millibitcoin) using the Coinbase API. Here is my code:
<?php
$string = file_get_contents('https://coinbase.com/api/v1/prices/spot_rate');
$result = json_decode($string);
$spot = $result->amount;
$price = 2; //change this to your USD value
$whole = substr($price/$spot, 4, -13);
$dec = substr($price/$spot, 4, -12);
echo $whole.'.'.$dec.' mBTC';
?>
It works flawlessly in Coderunner (OS X app for development) but fails when run on my hosting server. Link to browser script: http://bitcoindecals.com/oval-price.php
I am using Dynadot Advanced hosting and it includes PHP support. I know that PHP is being utilized because "mBTC" is being echoed correctly. It just appears the $whole and $dec variables aren't being set for some reason. Is there a way to get this to work?
You're making a few (extremly wrong) assumptions in the following lines:
$whole = substr($price/$spot, 4, -13);
$dec = substr($price/$spot, 4, -12);
$price / $spot is treated as a string and you assume it will be in the format
'0.0019XXXXXXXXXXXX' // 12 x's (unkown numbers)
What if Bitcoin is doing really bad and the rate exeeds 10 mBTC per USD? $price / $spot will be something like:
'0.0108491827849201'; // (10.8 mBTC)
$whole = substr('0.0108491827849201', 4, -13); // Will be '0'
$dec = substr('0.0108491827849201', 4, -12); // Will be '08'
echo $whole . '.' . $dec . ' mBTC'; // Will echo '0.08 mBTC'
What if, due to rounding or accuracy (! this is what you're seeing in your server - most likely because your OSX is 64bit, your server 32bit or vice-versa !), the string-length of $price / $spot is less than 18 characters:
'0.0019564521431';
$whole = substr('0.0019564521431', 4, -13);
// Meaning: start at position 4, stop at 13 characters counting from the end
// 13 characters from the end is here: '0.0019564521431'
// ^
// so the stop-position is before the start-position, resulting in an empty
// string. Same with $dec.
echo $whole . '.' . $dec . ' mBTC';
// Will echo empty-string . '.' . empty-string . ' mBTC': '. mBTC'
Long story short: never ever treat numbers as a string (unless you have no other options and you are fully aware of what you are doing). Following code will work, and will give the correct output:
echo number_format($price / $spot * 1000, 1);
// multiply by 1000: BTC to milli-BTC
// , 1: One digit after the dot
For a full explanation of number_format see: http://php.net/number_format
I just executed the exact code in my WAMP server on windows....
It seemed to work perfectly smooth !!!
OUTPUT : 1.19 mBTC Is it what you expect????
I think your server is unable to access web pages, so, your php code variables are going vacant...try XAMP and check out ....
Thanks....
you can't crawler the data from https://coinbase.com/api/v1/prices/spot_rate
so $string is empty!
do {
$string = file_get_contents('https://coinbase.com/api/v1/prices/spot_rate');
} while (!empty($string));
$result = json_decode($string);
$spot = $result->amount;
$price = 2; //change this to your USD value
if (isset($spot) or $spot == 0) {
echo "\$spot is not islet or 0";
} else {
$whole = substr($price/$spot, 4, -13);
$dec = substr($price/$spot, 4, -12);
echo $whole.'.'.$dec.' mBTC';
}
I have a field in my table that contains numbers for instance: 7121968 - what I would like to do is add a . before the last four and before the last 6 digits so that it looks like: 7.12.1968.
I can use substring to just show the last 4 digits etc - is what i am trying to do possible using substring and if so could someeone point me in the right direction? thanks.
$x = '7121968';
$x2 = substr($x,0,-6) . '.' . substr($x,-6,2) . '.' . substr($x,-4);
Do like this:
SELECT CONCAT(SUBSTRING(<column>, 1, 1), '.', SUBSTRING(<column>, 2, 2), '.', SUBSTRING(<column>, 4)) FROM <table>
I am having one weird problem.I am getting time of the day like 800 , 1200 ...
i want output String like 8:00,12:00..
I know alternatives but can anyone suggests me shortest way to complete it ..?plz
One way to do it:
echo preg_replace('^(\d{1,2})(\d{2})$', '$1:$2', $time);
Another way
echo substr($time, 0, 3 === strlen($time) ? 1 : 2) . ':' . substr($time, -2);
Or you can normalize the length
$time = str_pad($time, 4, 0, STR_PAD_LEFT);
echo substr($time, 0, 2) . ':' . substr($time, 2, 2);
if your source is string like "800" or "1200" then solution by deceze will do :
preg_replace('^(\d{1,2})(\d{2})$', '$1:$2', $time);
but if it's matter of date formatting then this is the right way to do it
date("H:i");
I have these value stored in a decimal 10,2 field
1052730
956700
How do i print this using php so that the value is like
$10,527.30
$9,567.00
basically i am trying to avoid the value as
$1,052,730 <--- this i dont want
You can use the
money_format($format, $value)
function in php. The details of the formatting is given here.
Well, assuming that 1052730 is really 10527.30 as alluded to in your question:
$number = 1052730;
$decimals = $number % 100; //30 in this case
$digits = floor($number / 100);
$paddedDecimals = str_pad($digits, 2, '0', STR_PAD_LEFT);
$out = '$' . number_format($digits, 0).'.'.$paddedDecimals;
echo $out; // $10,527.30
There are no floating point calculations used for the decimal part, so there's no need to worry about precision issues (although at this precision it would likely be hard to get a float error in there)...
Just divide by 100:
<?php
echo number_format(1052730/100, 2, '.', ',') . PHP_EOL;
echo number_format(956700/100, 2, '.', ',') . PHP_EOL;
printf ("$%01.2f", ($input / 100));