need a little help with a small issue of string splitting.
I'm trying to split a serial number into two, do some calculations on the second half and join it back to the first half. My problem is the second half starts with two zeros and PHP removes the leading zeros.
I think keeping the variables as strings will keep the zeros but I can't seem to find a way to split the serial number into smaller strings, all the methods I try split them into an array. Here is a part of my code;
$info1 = nkw549blc003i00021; //this is the serial number.
I want to split $info1 into;
$number1 = nkw549blc003i0
$number2 = 0021
then use for loop on $number2 like
$num = 1;
for ($num=1; $num < $unitsquantity[$key] ; $num++) {
$sum = $number2+$num;
$final=$number1.$sum;
echo "$final<br>";
}
Any help is greatly appreciated.
$info1 = 'nkw549blc003i00021';
$number1 = substr($info1, 0, -4);
$number2 = sprintf('%1$04d', substr($info1, -4, 4));
If the string will always be 4 chars long, you can use str_pad
for ($num=1; $num < $unitsquantity[$key] ; $num++) {
echo $number1 . str_pad($number2+$num, 4, '0', STR_PAD_LEFT);
}
Strings are array chars, so you can get each char of them by iterating through their length
define('SERIAL_NUM_LEN', 4);
$info1 = 'nkw549blc003i00021';
$number1 = ''; $number2 = '';
for ($i = 0; $i < strlen($info1)-SERIAL_NUM_LEN; $i++) {
$number1 .= $info1[$i];
}
for ($i = strlen($info1)-SERIAL_NUM_LEN; $i < strlen($info1); $i++) {
$number2 .= $info1[$i];
}
var_dump($number1, $number2);
Output:
string 'nkw549blc003i0' (length=14)
string '0021' (length=4)
This way you can skip whichever chars from the string you want if you want to build totally different string. Or add chars in the middle.
Related
I am printing the number of rows in a DBF file and this works:
$content = fread($fp, 8);
$number = 0;
for ($i = 7; $i >= 4; $i--) {
$number = $number * 256 + ord($content[$i]);
}
print $number;
But I am reasonably sure there is a pack or unpack command which would do the same. Is there?
It looks like $content contains an 8 byte string and you want to ignore the first 4 bytes and take the last 4 bytes as an unsigned int. If that's the case, you can use:
$number = unpack('L', substr($content, 4))[1];
I've got a long string consisting of HEX-characters:
$hex = '004d41494e0000000048001B030C1A050EEFF500'.
'F5FB0000400804FBF101F1F201F2F301F3FA00FA' .
'48454c4c4fFA00FAFB0008400804FBF101F1F201F2F30' .
'2F3574f524c44EF';
From this string I have to calculate a 'verify' by adding up the bytes within this string. And afterwards perform a NOT-operation on it.
In the documentation (this is a string to control a serial device) the verify should be 1C5B and after the NOT it should be E3A4
As I'm completely new to HEX I've got no clue how to tackle this 'problem'. I found a way to perform a NOT operation in php: ~ . But how to sum up the data in hex and end up with 1C5B.....
$hex_array = str_split($hex, 2);
foreach ($hex_array as $byte) {
$sum += hexdec($byte);
}
echo "Sum: ", $sum, "\n";
echo dechex($sum);
EDIT
Explanation:
Assuming the input string has even number of characters we can start from the first byte and take two hex characters (representing one byte). That's what str_split does. Then we loop over each of that two-character substrings and parse them to number using dechex that we then add to the main sum. After that the only thing left is to represent that sum as hex number, using hexdec.
References from php.net:
array str_split ( string $string [, int $split_length = 1 ] )
string dechex ( int $number )
number hexdec ( string $hex_string )
<?php
$hex = '004d41494e0000000048001B030C1A050EEFF500'.
'F5FB0000400804FBF101F1F201F2F301F3FA00FA' .
'48454c4c4fFA00FAFB0008400804FBF101F1F201F2F30' .
'2F3574f524c44EF';
$n = strlen($hex);
$sum = 0;
for ($i = 0; $i < $n; $i+= 2)
{
//fetch 2 chars from string
$byte = $hex[$i].$hex[$i + 1];
$byte = intval($byte, 16);
$sum += $byte;
}
//0x1C5B;
print sprintf('0x%4X', $sum)."\n";
$sum = ~$sum;
//To avoid the problem: prints FFFFE3A4, and not E3A4
$sum = $sum & 0xFFFF;
//E3A4
print sprintf('0x%4X', $sum)."\n";
I'm trying to add a 1 in front of my binary code and this is how I'm going about it:
if I have 0101, for example, then I'd add a number with 4 zeroes, like 10000 so it would become 10101. Here's my code:
$fill = strlen($string);
$number = '1';
$add = str_pad($number, $fill, '0', STR_PAD_RIGHT);
$m1 = $string + $add;
The problem is the output for that is something like 1.random number e+Random number
assuming $string is your "0101" string, you could just do $m1 = '1'.$string;
My previous answer was wrong because the length of the string is potentially variable and str_pad requires you to know the length. This will work, but it doesn't look so elegant:
if (strpos($string, '0') === 0) {
$string = '1' . $string;
}
How do I insert a random string of zeros (length between x and x+y) before each of the four digits for the code snipet below?
an example would be:
$quotes=array("000350.00155.062.00000044");
<?php
$quotes=array("$random+00350.$random2+0155.$random3+062.$random4+044");
Something like this would work, I don't completely understand your array declaration however and may have missed the point.
$quotes = array("00350","0155","062","044");
foreach($quotes as $i => $v) {
$a = rand($x, $x + $y);
$zeros = "";
for($j = 0; $j < $a; $j++) $zeros .= "0";
$quotes[$i] = $zeros . $v;
}
I would run a pseudo random over an uniform distribution [0-1]. Then, ceil of the number would be my number of zeros.
Done! :)
if you only have the $quotes variable I would you suggest you to do this:
-> explode $quotes (using the "explode" function and puting the "." as the delimiter)
-> get a random integer from x to x+y using rand(x, x+y)
-> concatenate the parts using a loop
Hie guys i want to create a random string of numbers where there is a fixed letter B at the beginning and a set of eight integers ending with any random letter, like for example B07224081A where A and the other numbers are random. This string should be unique. How can I do this?
Do you mean something like this?
$letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$numbers = rand(10000000, 99999999);
$prefix = "B";
$sufix = $letters[rand(0, 25)];
$string = $prefix . $numbers . $sufix;
echo $string; // printed "B74099731P" in my case
The more characters - the greater chance to generate unique string.
I think that's much better method to use uniqid() since it's based on miliseconds. Uniqueness of generated string is guaranteed.
This should work for you.
$randomString = "B";
for ($i = 0; $i < 9; $i++) {
if ($i < 8) {
$randomString.=rand(0,9);
}
if ($i == 8) {
$randomString.=chr(rand(65,90));
}
}
echo $randomString;