keep preceeding zeros when working with a integer - php

I'm using this code:
$start = substr($string, 0, -6);
$end = substr($string, -6);
for($i = 0; $i < 500; $i++) {
if($end > 500) {
echo $start.$end--."<br />";
} else {
echo $start.$end++."<br />";
}
}
Where $string will something like RXV123456 always last 6 characters will be a number.
If $string is RXV123456, the output will be someting like:
RXV123456
RXV123455
RXV123454
...
But, if $string will be something like RXV012345, I get this output:
RXV12345
RXV12344
RXV12343
Also if $string will be RXV001234 or RXV000123, same thing, that zeros will be omitted.
Any ideas how to keep the zeros if $end will start with one ore more zeros?

Any programming language will truncate zeros on the left side of an integer. If you want them to display, you can use a function like str_pad to add them when concatenating with the other string.

Related

How to add <br> in string with substr()

For the following code :
<?php
$word = 'SEKISUI';
// echo substr($word,0,-6)."<br>";
$length = (strlen($word)+1)-14;
$urut = 0;
for($i=$length;$i<1;$i++){
echo substr($word,$urut,$i).'<br>';
// echo $urut."-".$i."-".'<br>'; // Check Value
$urut++;
}
?>
Result :
S
E
K
I
S
U
why the letter "i" doesn't appear?
what is wrong with my code?
The result should look like:
S
E
K
I
S
U
I
Thank you for your attention...
I don't know if you NEED to use a 'for loop', but there is a better way to split a string into single characters.
Once you have the array you can do any operation to it, also join() the items in the array with a "\< br>" separator.
Try the following:
$word = 'SEKISUI';
$result = str_split($word);
$altogether = join("<br>",$result);
Not sure why you like the subtracting the length and not deal with positive numbers as much as possible.
In the syntax of substr(string,start,length),
Optional. Specifies the length of the returned string. Default is to
the end of the string. A positive number - The length to be returned
from the start parameter Negative number - The length to be returned
from the end of the string
So essentially your pointer on the end character is nevel counted.
if you run echo substr($word,0,-1)."<br>";, you will not get the end character as it is place of the start for the negative substr.
However, changing the substr length to 1 will give a valid string and not null or empty string
$word = 'SEKISUI';
// echo substr($word,0,-6)."<br>";
$length = (strlen($word)+1)-14;
$urut = 0;
for($i=$length;$i<1;$i++){
echo substr($word,$urut,1).'<br>';
// echo $urut."-".$i."-".'<br>'; // Check Value
$urut++;
}
However, I would prefer this approach, as this is much simpler.
$word = 'SEKISUI';
//echo substr($word,1,1)."<br>";
$length = strlen($word);
$urut = 0;
for($i = $urut; $i <= $length; $i++){
echo substr($word,$i,1).'<br>';
}

Split a string into smaller strings and not arrays

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.

Fill the remainder of a string with blank spaces

I have a string with, for example, 32 characters. For first, i want to establish that the string have max 32 characters and i want add blank spaces if characters is only, for example, 9.
Example:
ABCDEFGHI ---> 9 characters
I want this:
ABCDEFGHI_______________________ ---> 9 characters + 23 blank spaces added automatically.
The function you are looking for is str_pad.
http://php.net/manual/de/function.str-pad.php
$str = 'ABCDEFGHI';
$longstr = str_pad($str, 32);
The default pad string already is blank spaces.
As your maximum length should be 32 and str_pad won't take any action when the string is longer than 32 characters you might want to shorten it down using substr then:
http://de.php.net/manual/de/function.substr.php
$result = substr($longstr, 0, 32);
This again won't take any action if your string is exactly 32 characters long, so you always end up with a 32 characters string in $result now.
Use str_pad:
str_pad('ABCDEFGHI', 32);
Use str_pad function:
$result = str_pad($input, 32, " ");
you need str_pad
$padded = str_pad($in,32,' ');
You can have left or right padded, tons of options, check them all out here
If the input exceeds 32 chars, no action is taken, but that's an easy check to implement:
if (strlen($padded) > 32)
{
throw new Exception($padded.' is too long');//or some other action
}
If you want to do more customization inside the function you could use this
function fill($input, $length, $filler = ' ')
{
$len = strlen($input);
$diff = $length - $len;
if($diff > 0)
{
for ($i = 0; $i < $diff; $i++)
{
$input = $input . $filler;
}
}
return substr($input, 0, $length);
}
for($i=0;$i<(32-strlen($your_string));$i++)
{
$new_string.=$your_string.' '
}
hope that would be helpful for you
$str = "ABCDEFGHI";
for ($i = 0; $i < 23; $i++) {
$str .= " ";
}
This is what You want?
When saw other comments, then that will be the best solution.

PHP: Generating random string with both suffix and prefix as capital letters

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;

How to extract substring by start-index and end-index?

$str = 'HelloWorld';
$sub = substr($str, 3, 5);
echo $sub; // prints "loWor"
I know that substr() takes the first parameter, 2nd parameter is start index, while 3rd parameter is substring length to extract. What I need is to extract substring by startIndex and endIndex. What I need is something like this:
$str = 'HelloWorld';
$sub = my_substr_function($str, 3, 5);
echo $sub; // prints "lo"
Is there a function that does that in php? Or can you help me with a workaround solution, please?
It's just math
$sub = substr($str, 3, 5 - 3);
The length is the end minus the start.
function my_substr_function($str, $start, $end)
{
return substr($str, $start, $end - $start);
}
If you need to have it multibyte safe (i.e. for chinese characters, ...) use the mb_substr function:
function my_substr_function($str, $start, $end)
{
return mb_substr($str, $start, $end - $start);
}
Just subtract the start index from the end index and you have the length the function wants.
$start_index = 3;
$end_index = 5;
$sub = substr($str, $start_index, $end_index - $start_index);
You can just use a negative value on the third parameter:
echo substr('HelloWorld', 3, -5);
// will print "lo"
If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative).
As stated at the substr documentation.
Not exactly...
If we have a start index as 0, and we want JUST the first char, it becomes difficult as this will not output what you want. So if your code is requiring an $end_index:
// We want just the first char only.
$start_index = 0;
$end_index = 0;
echo $str[$end_index - $start_index]; // One way... or...
if($end_index == 0) ++$end_index;
$sub = substr($str, $start_index, $end_index - $start_index);
echo $sub; // The other way.

Categories