I want to add "+" character to a string "donalddukeoflagos" multiple times at random positions.
Donalddukeoflagos will now be don+alddu+ke+ofl+agos
The code below is working very well, but it can only add one character at a time.
<?php
$mailad = "Donalddukeoflagos";
$length = (strlen($mailad) - 1);
$insertposition = rand(1,$length);
echo substr_replace($mailad,"+",$insertposition,0);
?>
You can wrap it in a loop.
$specialCharacterCount = 5;
$mailad = "Donalddukeoflagos";
for ($specialCharacterIndex = 0; $specialCharacterIndex < specialCharacterCount; specialCharacterIndex++) {
$length = (strlen($mailad) - 1);
$insertposition = rand(1,$length);
echo substr_replace($mailad,"+",$insertposition,0);
}
Related
I want to capitalize random letters in a string and echo the complete sentence from hello69.world to HeLlO69.WoRlD. Each time the functions run, it should capitalize random letters and exclude special characters and digits. I've tried this so far but it only selects the first 5 characters in string every time and outputs only Capitalized letters. How to fix this?
<?php
$string = "hellohg.09ui8vkosjbdh";
$selchr = substr($string,0, 5);
$caps = strtoupper($selchr);
echo substr_replace($string, $caps,0);
?>
Let's assume you want to CAPITALIZE 5 letters randomly:
$string = "hellohg.09ui8vkosjbdh";
$characters = str_split($string);
$i = 0;
do{
$random_index = rand(0, count($characters) - 1);
$unique_indices[] = ""; //UNIQUE INDICES
while (in_array($random_index, $unique_indices)) {
$random_index = rand(0, count($characters) - 1);
}
$unique_indices[] = $random_index;
$random_letter = $characters[$random_index];
if(ctype_alpha($random_letter)){//only letters
$characters[$random_index] = strtoupper($random_letter);
$i++;
}
}while($i<5);echo implode('', $characters);
Thanks to #El_Vanja for Note UNIQUE INDICES
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>';
}
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++;
}
}
So I wrote the following code to show the words after the fourth full stop / period in a sentence.
$text = "this.is.the.message.seperated.with.full.stops.";
$limit = 4;
$minText = explode(".", $text);
for($i = $limit; $i < count($minText); $i++){
echo $minText[$i];
}
The algorithm is working and it is showing me the rest of the sentence after the fourth "." full stop / period.... My problem is that the output is not showing the full stops in the sentence therefore it is showing me just text without the proper punctuation "." .... Can someone please help me out on how to fix the code to display also the full stops / periods ??
Thanks a lot
you could try this...
for($i = $limit; $i < count($minText); $i++){
echo $minText[$i].".";
}
notice the added period at the end of the echo command // .".";
$text = "this.is.the.message.seperated.with.full.stops.";
$limit = 4;
$minText = explode(".", $text);
for($i = $limit; $i < count($minText); $i++){
echo $minText[$i].".";
}
Instead of splitting the input string and then iterating over it, you can find the nth position of the separator (.) in the string by using strpos() function by changing the offset parameter.
Then, it is just the matter of printing the sub-string from the position we just determined.
<?php
$text = "this.is.the.message.seperated.with.full.stops.";
$limit = 4;
$pos = 0;
//find the position of 4th occurrence of dot
for($i = 0; $i < $limit; $i++) {
$pos = strpos($text, '.', $pos) + 1;
}
print substr($text, $pos);
If desired output is "seperated.with.full.stops.", then you can use:
<?php
$text = "this.is.the.message.seperated.with.full.stops.";
$limit = 4;
$minText = explode(".", $text);
$minText = array_slice($minText, $limit);
echo implode('.', $minText) . '.';
If you want to break it up on the periods between words, but keep the one at the end as actual punctuation, you may want to use preg_replace() to convert the periods to another character and then explode it.
$text = "this.is.the.message.seperated.with.full.stops.";
$limit = 4;
//replace periods if they are follwed by a alphanumeric character
$toSplit = preg_replace('/\.(?=\w)/', '#', $text);
$minText = explode("#", $toSplit);
for($i = $limit; $i < count($minText); $i++){
echo $minText[$i] . "<br/>";
}
Which Yields
seperated
with
full
stops.
Of course, if you just simply want to print all the full stops, then add them in after you echo the term.
echo $minText[$i] . ".";
I want to swap 2 characters in a string with 2 other ones.
Start string = "`bHello `!how `Qare `%you."
Random string = "1234567890abcdefghijklmnopqrstuvwxyz!£$%^&#"
How do i swap `b `! `Q `% with random ones so it looks something like this
End result string = "`4Hello `^how `$are `#you."
I have tried this so far
I tried so far
$out = "`vHow `!are `#you."
$patterns = array("`1","`J","`2","`3","`4","`5","`6","`7","`!","`$","`%","`^","`&","`)","`~","`#","`#","`q","`e","`y","`t","`p","`j","`k","`l","`M","`x","`v","`m","`Q","`E","`R","`T","`Y","`P","`G","`K","`L","`X","`V");
$pretest = array("`1","`J","`2","`3","`4","`5","`6","`7","`!","`$","`%","`^","`&","`)","`~","`#","`#","`q","`e","`y","`t","`p","`j","`k","`l","`M","`x","`v","`m","`Q","`E","`R","`T","`Y","`P","`G","`K","`L","`X","`V");
$tempstr = $pretest[rand(0, strlen($pretest)-1)];
$substs = "`".$tempstr;
$out = preg_replace($patterns, $substs, $out);
However the end result is
$out = "`%How `%are `%you."
it picks only 1 random and changes them all to that one.
<?php
function randomChar() {
$rand = "1234567890abcdefghijklmnopqrstuvwxyz";
return substr($rand, rand(0, strlen($rand)), 1);
}
echo preg_replace_callback("/`./", 'randomChar', "`bHello `!how `Qare `%you.");