In php if I writ
$c='A';
$c++;
it increments to 'B' but if I want to increment it with 2 ,3 or more ,
eg: $c+2 or $c+3 ,to get the alternate alphabets
for ($column = 'B'; $column < $highestColumn; $column++) {
$cell = $worksheet->getCell($column.$row);
$cell2=$worksheet->getCell($column+1.$row);
}
but the $column+1 dont work
how to do that ?
Incrementing letters will only work with the ++ incrementor operator, not with + addition.
If you want to continue using an incrementor, you can use a loop:
$column = 'AH';
$step = 7; // number of columns to step by
for($ = 0; $i < $step; $i++) {
$column++;
}
However, PHP's character incrementor won't work backwards, so you couldn't use a negative step value
If you want to use addition rather than a loop, then you need to convert that column to a numeric value, do the addition, then convert back
$column = 'AH';
$step = 7; // number of columns to step by
$columnNumber = PHPExcel_Cell::columnIndexFromString($column) + $step;
$column = PHPExcel_Cell::stringFromColumnIndex($columnNumber - 1);
Which has the added benefit of allowing you to use negative step values
If you want to increment the character by 2, adding won't work.
Try this instead:
echo chr(ord($c) + 2)
Explanation:
Calculate the ascii value of $c using ord($c)
Add the ascii value by 2.
Convert this achii value to string using chr() function.
Refer to ord() and chr() functions.
Note:
As Mark Baker specifies, this will only work to Z and not beyond.
Related
I want to increment a letter by 2 each time, e.g.
// increment by 1
$alphabet = "A";
$alphabet++; // 'B'
I want something like
// increment by 2
$alphabet = "A";
$alphabet+=2; // 'C'
How can i do this? I tried the code above but encounter non numeric value.
You can have an array with the alphabet created by range.
The just echo the one you need with a counter.
$alphabet = range("A", "Z");
$i =0;
Echo $alphabet[$i];
$i =($i+25)%26;
Echo $alphabet[$i];
https://3v4l.org/U49OY
Edit Mark has a good point in comments above.
Added a mod calculation to keep it between A-Z.
In the code it's the ($i+25)%26; that is the increment value.
Use php chr and php ord
$alphabet = "A";
$ascii = ord($alphabet);
$ascii += 2;
echo $alphabet = chr($ascii);
LIve demo : https://eval.in/907132
I have an php code that writes an excel file with a variable number of columns. Each 4 rows, on the 5th I want to put the total of the four rows above, per column.
My issue is on how to write the formula in terms of columns.
My code is this one:
$col_index=20;
$i=20;
for($anno = $annoMin; $anno<=$annoMax; $anno++){
for($mese = 1; $mese <= 12; $mese++){
$string = "=$i$v+$i$q-$i$z-$i$t";
$ews->setCellValueByColumnAndRow($col_index,$k,$string);
$col_index=$col_index+1;
$i=$i+1;
}
}
In $string I need to put the column letter instead of $i. $v,$q,$z and $t are the reference to the rows and are ok. In other words $string should be evaluated to:
$string = "=U5+U3-U2-U4";
during the first for loop,
$string = "=V5+V3-V2-V4";
in the second and so on.
I know I can build an array of columns letter and use that $i reference to get my goal but I'm sure there is a better approach. I am using php 5.5 btw
The other option is to write the formula with the R1C1 notation but I'm pretty sure I cannot have the standard and the R1C1 notation in an excel sheet at the same time
You can increment a string variable. I think this is the most efficient way to do what you need (if I understood it right).
$ch = "a";
++$ch;
echo $ch; //prints "b"
Knowing this you can build a loop to update the value as you need.
This works very good for Excel because:
$ch = "z";
++$ch;
echo $ch; //prints aa
I'm just concentrating on the column letter as that seems to be what you're asking. Use the ASCII code of the letter and increment it, then convert it to the character:
$col_index = 20;
$i = ord('U'); // 85
for($anno = $annoMin; $anno<=$annoMax; $anno++){
for($mese = 1; $mese <= 12; $mese++){
$c = chr($i);
$string = "=$c$v+$c$q-$c$z-$c$t"; // $i will be U then V etc...
$ews->setCellValueByColumnAndRow($col_index,$k,$string);
$col_index = $col_index+1;
$i++;
}
}
If you need to keep $i starting at 20 and incrementing then just add 65:
$col_index = 20;
$i = 20;
for($anno = $annoMin; $anno<=$annoMax; $anno++){
for($mese = 1; $mese <= 12; $mese++){
$c = chr($i + 65);
$string = "=$c$v+$c$q-$c$z-$c$t"; // $i will be U then V etc...
$ews->setCellValueByColumnAndRow($col_index,$k,$string);
$col_index = $col_index+1;
$i++;
}
}
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
I have a text file, and I need to pick a random string that is over 6 characters and under 10 characters. Normally, I would use a script like this, which would work, but since it needs to be a certain length, that won't work. Does anybody have a solution to this?
A sample input would be something like this:
Apple
Banana
Orange
Strawberry
Blueberry
Pineapple
Somelongfruithere
Those values would be in a .txt file, each with a line break. An example of a string that would be allowed is pineapple, but apple or Somelongfruithere wouldn't be allowed.
You'll need to do something like this:
$lines = array();
$tmpLines = file('random.txt');
for($i = 0; $i < count($tmpLines); ++$i)
{
if(strlen($tmpLines[ $i ]) > 6 && strlen($tmpLines[ $i ]) < 10)
{
$lines[] = $tmpLines[ $i ];
}
}
$randomWord = $lines[ array_rand($lines) ];
A shorter way, in number of lines, goes like this (but is much less safe):
$randomWord = '';
$lines = file('random.txt');
while(strlen($randomWord) <= 6 || strlen($randomWord) >= 10)
$randomWord = $lines[ array_rand($lines) ];
The first option gets all the lines in the file, and copies only the ones between 6 and 10 chars in length to another array. When choosing a random element from this array, you are "guaranteed" a reasonable access time for any random string.
The second option simply continues to pick a random string until one of the proper length is chosen, but could potentially take a while depending on the random number generator's mood. Unlikely, but I wouldn't want to risk it. Always take reliability as the best approach, in my book.
I would say explode the text file into a variable, run your random generator to get a placement value (position of the random string) then in a loop (a do/while loop), pull the string from the exploded variable, and check it's length to ensure it's what you want
if (strlen($rand_word) > 6 && strlen($rand_word) < 10) {
//execute function and end loop
} else {
// keep checking using a new random placement number
}
The answer depends on the requirement.
If you need to select from a group of strings and only accept one that fits your criteria, then you'll need to use strlen and try again if it is not the correct length.
Otherwise, you're still going to need strlen, to make sure it is at least 6 Chars, but then you can use substr to cut it to 10. If whitespace does not count, use ltrim & rtrim before strlen and substr.
First find all the words that are in the right character range, then pull a random one from the resulting array:
$fileLines = file('somefile.txt');
$myWords = array();
foreach ($fileLines as $line)
{
$thisLine = split(" ",$line);
foreach ($thisLine as $word)
{
$length = strlen($word);
if ($length > 6 && $length < 10)
{
$myWords[] = $word;
}
}
}
$randomWord = $myWords[array_rand($myWords)];
Shortened way!
for ($i=7; $i<=9; $i++)
if (strlen($str) == $i )
echo "bingo! " . strlen($str);
i have a form with two text fields, "from" and "to."
when i enter values such as "100000" and "10000F" and do a for loop, it always comes out like:
0 100001 100002 100003 100004 100005 100006 100007 100008 100009 10000a 10000b 10000c 10000d 10000e 10000f
if i plug in the range for the loop manually, i get:
100000 100001 100002 100003 100004 100005 100006 100007 100008 100009 10000a 10000b 10000c 10000d 10000e 10000f
using:
for ($i = '0x'.$_POST['from']; $i <= '0x'.$_POST['to']; $i++) { print dechex($i)."\n"; }
versus:
for ($i = 0x100000; $i <= 0x10000F; $i++) { print dechex($i)."\n"; }
if anyone knows what i am doing wrong here, please let me know.
i have also tried tried adding the "0x" to the numbers via the form with the same results.
thanks!
While $i++ is converting $i to a number, this doesn't run until after the first loop. Apparently dechex doesn't try to interpret strings as numbers and just barfs.
To force conversion, prefix the string expression with a +:
for ($i = +('0x'.$_POST['from']); $i <= '0x'.$_POST['to']; $i++) {
print dechex($i)."\n";
}
Tested and working on PHP 5.2.6.
(Note that casting does not work! (int)('0x100000') returns 0.)
You should use intval() to convert a hex string to a number. dechex() will take the number and turn it back into a hex string:
$from = intval('100001', 16);
$to = intval('10000f', 16);
for ($i = $from; $i <= $to; $i++) {
print dechex($i) . "\n";
}
'0x'.$_POST['from']; is a string $i = 0x100000 is a number
Your first iteration will cast the string value to a number using standard PHP casting (as an integer value to the first non-numeric character, which is the 'x' giving a start value of 0)
You can also do
for ($i = intval('0x'.$_POST['from'], 0); $i <= intval('0x'.$_POST['to'], 0); $i++) { print dechex($i)."\n"; }
You need to convert your hex string to an actual integer first. Something like this function does.
Otherwise, PHP will handle it as a string.