How to create a Spinner in PHP? - php

I have problem and I hope you help me, please.
I have a code:
$a = 'A B C D E';
$b = '{A|a|AA}{B|b|BB} {E|e|EEE}';
And I want to use $b to random display $a like this:
A b C D EE
AA B C D e
A b C D EEE ...
This mean:
A replace to A, a or AA
B replace to B, b or BB
and
E replace to E, e or EEE
I hope you understand and help me, thankyou so much!!! <3

If you are not wedded to the format of $b, you could use arrays for substitutes, and pick a random item to replace.
$placeholders = 'A B C D E';
$substitutes = [
'A' => ['A','a','AA'],
'B' => ['B','b','BB'],
'E' => ['E','e','EE','EEE'],
];
$replacements = [];
foreach($substitutes as $key => $choices) {
$random_key = array_rand($choices);
$replacements[$key] = $choices[$random_key];
}
$spun = str_replace(
array_keys($replacements),
array_values($replacements),
$placeholders
);
echo $spun;
Example output:
AA b C D EE
Alternatively (if your substitutes are uniform):
function substitute($character) {
$random = rand(0,2);
$string = $random
? str_repeat($character, $random)
: strtolower($character);
return $string;
}
$spun = implode(
' ',
array_map(
'substitute',
['A','B','C','D','E']
)
);
echo $spun;
But the above will substitute for C and D also. You could easily adapt for exclusions.

Related

Get all the palindromes in a string

I was asked to write a program in php where all the occurrences of palindromes in a given string need to be printed.
Example: For the string I O M K I L O L I K T C J I O P L L P O
The answer would be:
O P L L P O and
K I L O L I K
While doing this it should kept in mind that every palindrome that is more than 3 characters in length can be broken down into more palindromes but you just need to print out the longest size possible for each set (so, for the example string, you SHOULD NOT print LOL, LL , PLLP and ILOLI)
I tried it but could only manage to do this:
$data = 'I O M K I L O L I K T C J I O P L L P O';
$data = str_replace(' ', '', $data);
$palindromes = [];
for($i=0; $i<strlen($data); $i++ ) {
for($j=3; $j<=(strlen($data)-$i); $j++){
$word = substr($data, $i, $j);
$reverse_word = strrev($word);
if($word == $reverse_word){
print "Word: ".$word."<br/>";
}
}
}
Which gives me the following output:
Word: KILOLIK
Word: ILOLI
Word: LOL
Word: OPLLPO
Word: PLLP
Which is not the expected ouput. What should I to get rid of the strings like ILOLI, LOL PLLP because I am expected to get the longest palindrome.
According your example, you mention the answer would be O P L L P O (six characters long) and K I L O L I K (seven characters long), but later you imply only the longest palindrome should be returned?
Assuming you mean the later case, you can first assign all the palindromes to array and then sort the array, like this:
function str_length_sort($first, $second) {
return strlen($second) - strlen($first);
}
$data = 'I O M K I L O L I K T C J I O P L L P O';
$data = str_replace(' ', '', $data);
$palindromes = [];
for($i=0; $i<strlen($data); $i++ ) {
for($j=3; $j<=(strlen($data)-$i); $j++){
$word = substr($data, $i, $j);
$reverse_word = strrev($word);
if($word == $reverse_word){
$palindromes[] = $word;
}
}
}
usort($palindromes, 'str_length_sort');
$max = strlen($palindromes[0]);
foreach ($palindromes as $p) {
$length = strlen($p);
if ($length >= $max) {
echo $p;
}
}
Hope this helps.

While randomly and not repeating twice

Here is my code:
$answer = explode(" ", $row2['answer']);
foreach($tags as $i =>$key) {
$i >0;
echo $i.' '.$key .'</br>';
}
the output is
0 a
1 b
2 c
3 d
4 e
5 f
I'd like that the output be random, but doesn't repeat twice.
For example:
e 4
a 0
c 2
f 5
d 3
b 1
Any idea please ?
Thank you.
The simplest way I can think of to achieve this would be to use the shuffle method on an array collection. This however does not guarantee non-sequentialness:
$range = range(1,5);
shuffle($range);
foreach($range as $int){
echo $int;
}

How to filter and sort a text-block into an array, in PHP?

I have a simple text-block with 3 columns:
id_1 text_1 A
id_9 text_2 B
id_2 text_5 D
id_3 text_3 G
id_8 text_4 C
id_4 text_9 X
id_0 text_0 X
id_5 text_8 Z
...
and a PHP array:
$std_string = array ("A", "B", "X", "C", "M");
I want to filter and sort the text block into a new text-block, based on the order and existed text in$std_string.
Exactly, it will return a new text-block:
id_1
id_9
[DU]
id_8
[NO]
If the 3rd column (of orginal text-block) doesn't have a value in 1st column (which is listed in $std_string) (like M), it will return [NO].
If the 3rd column (of orginal text-block) have two values in 1st column (like X), it will return [DU].
Is there any suggestion to do it, in PHP?
All data are case-sensetive.
Here it is:
$text_block="id_1 text_1 A
id_9 text_2 B
id_2 text_5 D
id_3 text_3 G
id_8 text_4 C
id_4 text_9 X
id_0 text_0 X
id_5 text_8 Z";
$std_string = array ("A", "B", "X", "C", "M");
$rows = explode("\n", str_replace("\r","",str_replace("\t"," ",$text_block)));
$letter_ids = array();
foreach($std_string as $letter) {
$letter_ids[$letter] = "[NO]";
}
for($i=0;$i<count($rows);$i++) {
$cols = explode(" ",trim($rows[$i]));
$letter = $cols[count($cols)-1];
if(isset($letter_ids[$letter])) {
if($letter_ids[$letter] == "[NO]") {
$letter_ids[$letter] = $cols[0];
} else {
$letter_ids[$letter] = "[DU]";
}
}
}
$new_text_block = "";
foreach($letter_ids as $key => $val) {
$new_text_block.= $val."\n";
}
echo "<pre>".$new_text_block."</pre>";
Output:
id_1
id_9
[DU]
id_8
[NO]

How to find contiguous words in array

ok, this is driving me insane, the solution must be easy but I've hit the proverbial wall,
here it is:
suppose I have this Array: a, b, c, d
I want to find all the contiguous letters in the array without mixing them, such as:
a b c d
a b c
a b
b c d
b c
c d
I've done many tests, but for some reason I just can't get it right. Any hint would be greatly appreciated.
$arr = Array('a', 'b', 'c', 'd');
for ($i = 0; $i < count($arr); $i++) {
$s = '';
for ($j = $i; $j < count($arr); $j++) {
$s = $s . $arr[$j];
if (strlen($s) > 1) {
echo $s.' ';
}
}
}
OUTPUT:
ab abc abcd bc bcd cd

PHP Array Combination - One to many

Sorry if a solution exists anywhere else, but I couldn't find one.
I have the following array:
$data = array(
array('a', 'b', 'c'),
array('e', 'f', 'g'),
array('w', 'x', 'y', 'z'),
);
I am trying hard to write a function that will give an array like:
a
e
w
x
y
z
f
w
x
y
z
g
w
x
y
z
b
e
w
x
y
z
f
w
x
y
z
g
w
x
y
z
c
e
w
x
y
z
f
w
x
y
z
g
w
x
y
z
The major problem here is that the number of source arrays and their lengths keep changing always. So a function should be capable of handling any data given to it.
I tried to come up with something like this:
function testfunc($data){
$arrayDepth = count($data);
foreach($data as $key=>$d){
foreach($d as $e){
echo $e . "\n";
if($key < $arrayDepth){
array_shift($data);
testfunc($data);
}
}
}
}
And the output I got was:
a
e
w
x
y
z
f
g
w
x
y
z
b
w
x
y
z
c
e
f
g
w
x
y
z
I am stuck for almost a day with no proper solution. Any help would be great! Thanks!
Recursion [Wikipedia] is your friend:
function product($arrays) {
if(count($arrays) === 1) {
return $arrays[0];
}
else {
$result = array();
foreach($arrays[0] as $value) {
$result[$value] = product(array_slice($arrays, 1));
}
return $result;
}
}
DEMO
Non-recursive version. This should run fast!
$result = end($data);
if ($result === false)
{
return false; // or Array or what makes sense for an empty array.
}
$higherArr = prev($data);
while ($higherArr !== false)
{
// Set the orignal array to be the one that you built previously.
$orig = $result;
$result = array();
foreach ($higherArr as $higherKey)
{
$result[$higherKey] = $orig;
}
$higherArr = prev($data);
}
echo 'Finished with: ' . var_export($result, true);

Categories