Generate .txt file with character increment t - php

I want to generate .txt file with following content
*.pdf.aa,*.pdf.ab,*.pdf.ac,*.pdf.ad............
*.pdf.ba,*.pdf.bb,*.pdf.bc,*.pdf.bd............
..............
..............
*.pdf.za,*.pdf.zb,*.pdf.zc,*.pdf.zd............
which is the best way to implement this ?How to increment a character like this
EDIT:
I tried below one
for($i = 'aa'; $i <= 'z'; $i++)
{ echo "$i ";}
Here its print upto yz only not printing za,zb ...
aa ab ac.......... yu yv yw yx yy yz

You can incerement characters but you can do it with range()
$letters = range('a', 'z');
foreach($letters as $l) {
foreach($letters as $l2) {
echo $l.$l2.PHP_EOL;
}
}
Working fiddle
The other option based on #gbestard suggestion
for($a = 'aa' ; $a <= 'zz' && strlen($a) < 3 ; ++$a) {
echo $a.PHP_EOL;
}
Fiddle for second option

Change your for to:
for ($i = 'aa'; strlen($i) <= 2; $i++) {
echo $i;
}
for some reason neither $i <= 'zz' or $i < 'aaa' works, so just check the length of the string.

Related

Can a PHP for loop with number array start with NULL and also have 0?

I am working with some legacy PHP code so re-writing this isn't an option at this point but I have a dropdown for number of years and months of employment and currently they go from 0 - 11, 0 - 65. Can a PHP loop array numbers starting at NULL, which adds -Select- as the default forcing user to make a selection, but also have 0 as the starting number?
I've tried:
for ($i = NULL; $i <= 11; $i++) {
echo $i;
}
But 0 is no loner an option
This is what I have currently:
for ($i = 0; $i <= 11; $i++) {
echo $i;
}
I need it to display as:
-Select-
0
1
2
3
4
etc
Echo the text before echoing the numbers using a loop.
<?php
echo '-Select-';
for($i = 0; $i <= 11; ++$i) {
echo $i;
}
NULL++ would not increase the value of NULL, which is essentially nothing. Why not start at -1, and if ($i == -1) then echo select?
It would look like this:
for ($i = -1; $i <= 11; $i++) {
if ($i == -1) {
echo '-Select-';
} else {
echo $i;
}
}

How to print letters from A-Z in a for loop?

I am trying to print out alphabets from 'A' to 'Z'using for loop. What I could do is :
foreach (range('A', 'Z') as $char) {
echo $char . "\n";
}
but what I am trying to achieve is :
for($i = 'A'; $i <= 'Z'; $i++){
echo $i . "<br>";
}
The above method gives me a ridiculously long chain of alphabets.
Using,
for($i = 'A'; $i < 'Z'; $i++){ //note the change of '<=' to '<'
echo $i;
}
does give me alphabets A to Y.
When $i reaches Z, it's still <= Z, so it echoes out and increments, then tests again to see if the result of that is <= Z.... the problem is that PHP uses Perl-style character incrementing.... incrementing Z gives AA, and AA <== Z is true in an alphabetic comparison, so it continues echoing, incrementing and testing through AB, AC, to AZ, BA, BB etc.....
it's only when it reaches YZ that the next increment gives ZZ which isn't <= Z and it terminates
The solution is to avoid using a <= comparison, but to use a !== comparison, and that needs to be a comparison against the next increment from Z, ie AA, so
for($i = 'A'; $i !== 'AA'; $i++){
...
}
As uppercase alphabetical characters are following each other in the ASCII table, you can use the chr function
for ($i = 65; $i <= 90; $i++) {
echo(chr($i).' ');
}
65 is 'A' in the ASCII Table and 90 is 'Z'
For code clearness, you could either do : (using ord function)
for ($i = ord('A'); $i <= ord('Z'); $i++) {
echo(chr($i).' ');
}
You can stop loop AA. As the next element after Z is AA
for($i = 'A'; $i != 'AA'; $i++){ //note the change of '<' to '<='
echo $i."\n";
}
With simple do-while loop:
$l = "A";
do {
echo $l;
} while($l++ != "Z");
The output:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Try like below
$apl_arr = range('A', 'Z');
for($ii=0;$ii<count($apl_arr);$ii++)
echo $apl_arr[$ii],',';
exit;
may this will help you
Sorry i made a mistake in my previous answer, Please try this:
<?php
for ($i = 'A'; $i !== 'AA'; $i++)
echo "$i\n";
?>
I think you should specify the language you are trying to get this printed with. The simplest answer I can think of is in bash with the following:
for i in {A..Z}; do echo $i; done

How to prepend a decremented number to a string in a loop?

I am trying to make a triangular-shaped set of lines of decreasing numbers like this :
5
45
345
2345
12345
I tried this :
for($i=1;$i<=5;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo $j;
}
echo "<br>";
}
But it is printing the low number first and appending increasing numbers like this :
1
12
123
1234
12345
The inner loop needs to count down instead of up.
You can either subtract the outer loop's variable from the limit to get the starting point and count down:
for ($i = 0; $i < 5; $i++)
{
for ($j = 5 - $i; $j > 0; $j--)
{
echo $j;
}
echo "<br>";
}
or change the outer loop to count down from the limit as well.
for ($i = 5; $i >= 1; $i--)
{
for ($j = $i; $j >= 1; $j--)
{
echo $j;
}
echo "<br>";
}
This is pretty straightforward:
$max = 5;
echo "<pre>";
for($line=0; $line<$max; $line++) {
$min_this_line = $max-$line;
for($num = $min_this_line; $num <= $max; $num++) {
echo $num;
}
echo "\n";
}
echo "</pre>";
Output:
5
45
345
2345
12345
I think I would declare the $peak value, then use a for() loop to decrement the counter down to 1, and use implode() and range() to build the respective strings inside the loop.
This isn't going to outperform two for() loops, but for relatively small $peak values, no one is going to notice any performance hit.
Code: (Demo)
$peak = 5;
for ($i = $peak; $i; --$i) {
echo implode(range($i, $peak)) , "\n";
}
or with two loops: (Demo)
Decrement the outer loop and increment the inner loop.
$peak = 5;
for ($i = $peak; $i; --$i) {
for ($n = $i; $n <= $peak; ++$n) {
echo $n;
}
echo "\n";
}
Both output:
5
45
345
2345
12345

How to loop char in php

i need to do a simple loop in php to get char from a to z.. something like:
for($i=0;$i<aNumber;$i++)
echo "char = ".intToChar($i)."<br>";
where aNumber is less then 20
You can create an array of characters easily with range [docs]:
$chars = range('a', 'z');
Or if you really only want to print them:
echo implode('<br />', range('a', 'z'));
You need understand ASCII table. See in this link. Codes for A - Z is 65 - 90 and a - z is 97 - 122.
for($i = 97; $i <= 122; $i++)
echo chr($i);
I SOLVED IN THIS WAY:
$i = 0;
$char = 'a';
$aNumber = xx;
while($i<$aNumber){
echo $char."<br>";
$char++;
$i++;
}
To return character specified by ASCII code/number use chr function. Since you want to loop from 0 to 20, then you need to add 97 to $i, 97 is ASCII code for letter a:
for($i=0;$i<$aNumber;$i++)
echo "char = ".chr($i+97)."<br>"; // + 97 because it is ASCII 'a'
If you just want to loop through those letters you can do it like this:
for($i = ord('a'); $i < ord('z'); $i++)
echo "char = ".chr($i)."<br>";
Additionally if you just want to get array of those characters you can use range function:
$chars = range('a', 'z');

PHP function to loop thru a string and replace characters for all possible combinations

I am trying to write a function that will replace characters in a string with their HTML entity encoded equivalent.
I want it to be able to go through all the possible combinations for the given string, for example:
go one-by-one
then combo i.e.. 2 at a time, then three at a time, till you get length at a time
then start in combo split, i.e.. first and last, then first and second to last
then first and last two, fist and second/third last
So for the characters "abcd" it would return:
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
etc.......... so on and so forth till there are no other combinations
Any ideas, or has anyone seen a function somewhere I could modify for this purpose?
loop from 0 to 2^length - 1. On each step, if Nth bit of the loop counter is 1, encode the Nth character
$str = 'abcd';
$len = strlen($str);
for($i = 0; $i < 1 << $len; $i++) {
$p = '';
for($j = 0; $j < $len; $j++)
$p .= ($i & 1 << $j) ? '&#' . ord($str[$j]) . ';' : $str[$j];
echo $p, "\n";
}
There are 2^n combinations, so this will get huge fast. This solution will only work as long as it fits into PHP's integer size. But really who cares? A string that big will print so many results you'll spend your entire life looking at them.
<?php
$input = 'abcd';
$len = strlen($input);
$stop = pow(2, $len);
for ($i = 0; $i < $stop; ++$i)
{
for ($m = 1, $j = 0; $j < $len; ++$j, $m <<= 1)
{
echo ($i & $m) ? '&#'.ord($input[$j]).';' : $input[$j];
}
echo "\n";
}
How about this?
<?php
function permutations($str, $n = 0, $prefix = "") {
if ($n == strlen($str)) {
echo "$prefix\n";
return;
}
permutations($str, $n + 1, $prefix . $str[$n]);
permutations($str, $n + 1, $prefix . '&#' . ord($str[$n]) . ';');
}
permutations("abcd");
?>

Categories