How to find contiguous words in array - php

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

Related

Why this unexpected behavior in pushing letters [a-z] in PHP? [duplicate]

<?php
for ($i = 'a'; $i <= 'z'; $i++)
echo "$i\n";
This snippet gives the following output (newlines are replaced by spaces):
a b c d e f g h i j k l m n o p q r s t u v w x y z aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az ba bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq br bs bt bu bv bw bx by bz ca cb cc cd ce cf cg ch ci cj ck cl cm cn co cp cq cr cs ct cu cv cw cx cy cz da db dc dd de df dg dh di dj dk dl dm dn do dp dq dr ds dt du dv dw dx dy dz ea eb ec ed ee ef eg eh ei ej ek el em en eo ep eq er es et eu ev ew ex... on to yz
From the docs:
PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's.
For example, in Perl 'Z'+1 turns into 'AA', while in C 'Z'+1 turns into '[' ( ord('Z') == 90, ord('[') == 91 ).
Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.
From Comments:-
It should also be noted that <= is a lexicographical comparison, so 'z'+1 ≤ 'z'. (Since 'z'+1 = 'aa' ≤ 'z'. But 'za' ≤ 'z' is the first time the comparison is false.) Breaking when $i == 'z' would work, for instance.
Example here.
Because once 'z' is reached (and this is a valid result within your range, the $i++ increments it to the next value in sequence), the next value will be 'aa'; and alphabetically, 'aa' is < 'z', so the comparison is never met
for ($i = 'a'; $i != 'aa'; $i++)
echo "$i\n";
Others answers explain the observed behavior of the posted code. Here is one way to do what you want (and it's cleaner code, IMO):
foreach (range('a', 'z') as $i)
echo "$i\n";
In response to ShreevatsaR's comment/question about the range function: Yes, it produces the "right endpoint", i.e. the values passed to the function are in the range. To illustrate, the output from the above code was:
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Others already said why PHP doesn't show what you expect. Here's how you get the result you might want:
<?php
for ($i = ord('a'); $i <= ord('z'); $i++)
echo chr($i);
?>
Why not just use range('a','z')?
Try this code. I think this code will be helpful to you.
$alphas = range('A', 'Z');
foreach($alphas as $value){
echo $value."<br>";
}
Display 26 letters in sequence.
<?php
$i = 'a';
do {
echo ($j=$i++),"\r\n";
} while (ord($j) < ord($i));
?>
Also this can be used:
for ($i = 'a'; $i <= 'z'; $i=chr(ord($i)+1))
echo "$i\n";
PHP has the function of looping letters and can exceed beyond single characters; the rest will be done this way: aa ab ac... zz, and so on.
Try this:
<?php
for ($i = 'a'; $i !== 'aa'; $i++)
echo "$i\n";
?>
While the above answers are insightful to what's going on, and pretty interesting (I didn't know it would behave like this, and its good to see why.
The easiest fix (although perhaps not the most meaningful) would be just to change the condition to $i != 'z'
<?php
for ($i = 'a'; $i != 'z'; $i++)
echo "$i\n";
?>
The PHP does not consider 'AA' less than 'Z'.
The best way to make this is:
for($i = 'a'; $i != 'aa'; $i++) {
echo $i;
}
abcdefghijklmnopqrstuvwxyz
Perhaps this code will work. It’s easy & can be understood:
<?php
$ascii_val = ord("a");
for($i=$ascii_val;$i<$ascii_val+26;$i++){
echo chr($i)."\n";
}
?>
where 26 is the total number of letters in the alphabet.
There are several ways to do that.
1. First you can take the range from 'a' to 'z'. Then iterate a loop over it.
foreach(range('a', 'z') as $i)
{
echo $i . "\n";
}
2. You can print the letters using asci value of the characters.
for($i = 97 ; $i<=122; $i++)
{
echo chr($i) . "\n";
}
3. You can take the asci value of 'a' and run a loop till the asci value of 'z':
for ($x = ord('a'); $x <= ord('z'); $x++)
{
echo chr($x) . "\n";
}
4. For printing a new line in html you can append the to the end of each characters.
for($i='a';$i<='z';$i++)
{
echo $i. "<br />";
}
this code will work. It’s easy & can be understood:
<?php
// print form A to ZZ like this
// A B C ... AA AB AC ... ZA .. ZZ
$pre = "";
$i = ord('a');
for ($x = ord('a'); $pre . strtoupper(chr($x-1)) != 'AH'; $x++)
{
echo "". $pre . strtoupper(chr($x)) . "\n";
if(chr($x) === "z"){
$pre = strtoupper(chr($i++));
$x = ord('a');
$x--;
}
}
One more way to get the display of characters a-z:
<?php
for ($i = 'a'; $i < 'z'; $i++){
echo "$i\n";
}
echo $i; // 'z'
See live code
Once the conditional loop becomes false, $i will have already been incremented and have the string value corresponding to 'z' which can be displayed.
Wow I really didn't know about this but its not a big code you can try echo "z" after loop Mark is Absolutely Right I use his method but if you want alternative then this may also you can try
<?php
for ($i = "a"; $i = "y"; $i++) {
echo "$i\n";
if ($i == "z") {}
}
echo "z";
?>

Explode string by one or more spaces and new lines

How can I explode a string by one or more spaces or tabs?
Example:
A B C
D E F
G H I
J K L
M N O
I have successfully tokenize on spaces and tab's with this code:
$parts = preg_split('/\s+/', $str);
but the issue rises when there is new line.
I want to make this an array so that i can run mysqlquery as well.
If you want to get a matrix of characters, split by newline first:
<?php
$str = "A B\tC\nD E\tF";
$parts = preg_split("/\n+/", $str);
foreach($parts as $key => $line)
{
$parts[$key] = preg_split("/\s+/", $line);
}
//output
$n = count($parts);
for ($i = 0; $i < $n; ++$i)
{
$m = count($parts[$i]);
for ($j = 0; $j < $m; ++$j)
{
print $parts[$i][$j]." ";
}
print "\n";
}
Output:
A B C
D E F

PHP array_rand weird behaviour

I was trying to create a simple password generator and noted that array_rand() returns the same results. Here is the code:
<?php
function generatePass() {
$password = '';
$a = explode(' ', 'q w e r t y u i o p a s d f g h j k l z x c v b n m');
for($i=0; $i < rand(1,200); $i++) {
$password .= $a[array_rand($a)];
}
return $password;
}
$r = 0;
while ($r <= 10000) { #generating 10 000 passwords
$total[] = generatePass();
$r++;
}
echo '<pre>';
print_r($total);
echo '</pre>';
?>
The $total array basically contains the same results repeated over and over again; if I refresh the page, only the order of elements changes, and not their values.
The question is: is this an expected behaviour or am I doing something wrong?
Thak you for your attention.
Reseed the random number generator with srand() just prior to calling array_rand.
srand();
$password .= $a[array_rand($a)];
I think it should be your PHP version. I just copied your code into my localhost server and it worked fine. I'm using PHP 5.5.9-1ubuntu4.7, you should try it or a newer version.
By the way, if you can't update your PHP version, use this modified version of your code:
<?php
function generatePass() {
$password = '';
// Put all letters into an array.
$letters = array('q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m');
for($i=0; $i < rand(1,200); $i++) {
// Select a random letter with rand() command inside the brackets of the array.
$password .= $letters[rand(0,25)];
}
return $password;
}
$r = 0;
while ($r <= 10000) { #generating 10 000 passwords
$total[] = generatePass();
$r++;
}
echo '<pre>';
print_r($total);
echo '</pre>';
?>
Seeding the random generator isn't needed since version 4.2.0
Try using mt_rand() for a better generation.
http://php.net/manual/en/function.mt-rand.php
Edit:
I think you actually want
$a = explode(' ', 'q w e r t y u i o p a s d f g h j k l z x c v b n m');
$end = mt_rand(1,200);
$password = '';
for($i=0; $i < $end; $i++) {
$password .= $a[array_rand($a)];
}
I took the time for you and wrote the generator.
I got it to generate 10000 unique passwords and code is efficient:
<?php
function generatePass() {
$password = '';
$lib = 'q w e r t y u i o p a s d f g h j k l z x c v b n m';
$a = explode(' ', $lib);
//remove random char
for($i=0; $i < mt_rand(1,49); $i++) {
shuffle($a);
//get Random Char
$password .= $a[mt_rand(0,count($a)-1)];
}
return $password;
}
$len = 10000; // total number of numbers
$range = array();
foreach(range(0, $len - 1) as $i){
while(in_array($num = generatePass(), $range)){}
$range[] = $num;
}
echo '<pre>';
print_r($range);
echo '</pre>';

Trying to display a random generated string on my page

I'm trying to display a random generated string on my page (php), but I have absolutely no idea how to do this.
I only want the following letters and digits to be used:
B C D F G H J K M P Q R T V W X Y Z 2 3 4 6 7 8 9
In the following format:
XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
Can anyone help me out, and give me a script which I can put on my page? Help would be really appreciated!
I tried this but it's not even displaying on my page for some odd reason.
$tokens = 'BCDFGHJKMPQRTVWXYZ2346789';
$serial = '';
for ($i = 0; $i < 5; $i++) {
for ($j = 0; $j < 5; $j++) {
$serial .= $tokens[rand(0, 35)];
}
if ($i < 3) {
$serial .= '-';
}
}
echo $serial;
You were almost there. Here's a few fixes to your code;
<?php
$tokens = 'BCDFGHJKMPQRTVWXYZ2346789';
$serial = '';
for ($i = 0; $i < 5; $i++) {
for ($j = 0; $j < 5; $j++) {
$serial .= $tokens[rand(0, strlen($tokens) - 1)];
}
if ($i < 4) {
$serial .= '-';
}
}
echo $serial;
?>
I can't say for sure why your page isn't showing, but in your original code you were missing <?php at the top of the page.
Edit: Here's a quick explanation of some of the changes I made to your code.
Your code had rand(0, 35). But since you may change the characters in $tokens in the future, it's better to simply calculate the length of the $tokens using strlen($tokens) - 1 (the -1 being important because strlen() starts counting at 1, whereas $tokens[INDEX] starts counting at 0).
Your code had if ($i < 3), but you actually want four dashes, so I changed it to if ($i < 4).
<?php
$charsPerGroup = 5;
$groups = 5;
$groupDelimiter = '-';
$tokens = explode(' ', 'B C D F G H J K M P Q R T V W X Y Z 2 3 4 6 7 8 9'); // from your question, format this however you want
$tokens = array_flip($tokens);
$resultArray = array();
for($i=0;$i<$groups;$i++) {
$resultArray[] = join(array_rand($tokens, $charsPerGroup));
}
echo join($groupDelimiter, $resultArray);
<?php
$enters = explode(' ', "B C D F G H J K M P Q R T V W X Y Z 2 3 4 6 7 8 9");
$entry = rand(0,count($enters)-1);
echo $enters[$entry];
$output = "";
for($i=1; $i++; $i<=25) {
$entry = rand(0,count($enters)-1);
$output .= $enters[$entry] . ($i % 5 == 0 && $i < 25 ? '-' : '' );
}
echo $output;
?>

Why doesn't this code simply print letters A to Z?

<?php
for ($i = 'a'; $i <= 'z'; $i++)
echo "$i\n";
This snippet gives the following output (newlines are replaced by spaces):
a b c d e f g h i j k l m n o p q r s t u v w x y z aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az ba bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq br bs bt bu bv bw bx by bz ca cb cc cd ce cf cg ch ci cj ck cl cm cn co cp cq cr cs ct cu cv cw cx cy cz da db dc dd de df dg dh di dj dk dl dm dn do dp dq dr ds dt du dv dw dx dy dz ea eb ec ed ee ef eg eh ei ej ek el em en eo ep eq er es et eu ev ew ex... on to yz
From the docs:
PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's.
For example, in Perl 'Z'+1 turns into 'AA', while in C 'Z'+1 turns into '[' ( ord('Z') == 90, ord('[') == 91 ).
Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.
From Comments:-
It should also be noted that <= is a lexicographical comparison, so 'z'+1 ≤ 'z'. (Since 'z'+1 = 'aa' ≤ 'z'. But 'za' ≤ 'z' is the first time the comparison is false.) Breaking when $i == 'z' would work, for instance.
Example here.
Because once 'z' is reached (and this is a valid result within your range, the $i++ increments it to the next value in sequence), the next value will be 'aa'; and alphabetically, 'aa' is < 'z', so the comparison is never met
for ($i = 'a'; $i != 'aa'; $i++)
echo "$i\n";
Others answers explain the observed behavior of the posted code. Here is one way to do what you want (and it's cleaner code, IMO):
foreach (range('a', 'z') as $i)
echo "$i\n";
In response to ShreevatsaR's comment/question about the range function: Yes, it produces the "right endpoint", i.e. the values passed to the function are in the range. To illustrate, the output from the above code was:
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Others already said why PHP doesn't show what you expect. Here's how you get the result you might want:
<?php
for ($i = ord('a'); $i <= ord('z'); $i++)
echo chr($i);
?>
Why not just use range('a','z')?
Try this code. I think this code will be helpful to you.
$alphas = range('A', 'Z');
foreach($alphas as $value){
echo $value."<br>";
}
Display 26 letters in sequence.
<?php
$i = 'a';
do {
echo ($j=$i++),"\r\n";
} while (ord($j) < ord($i));
?>
Also this can be used:
for ($i = 'a'; $i <= 'z'; $i=chr(ord($i)+1))
echo "$i\n";
PHP has the function of looping letters and can exceed beyond single characters; the rest will be done this way: aa ab ac... zz, and so on.
Try this:
<?php
for ($i = 'a'; $i !== 'aa'; $i++)
echo "$i\n";
?>
While the above answers are insightful to what's going on, and pretty interesting (I didn't know it would behave like this, and its good to see why.
The easiest fix (although perhaps not the most meaningful) would be just to change the condition to $i != 'z'
<?php
for ($i = 'a'; $i != 'z'; $i++)
echo "$i\n";
?>
The PHP does not consider 'AA' less than 'Z'.
The best way to make this is:
for($i = 'a'; $i != 'aa'; $i++) {
echo $i;
}
abcdefghijklmnopqrstuvwxyz
Perhaps this code will work. It’s easy & can be understood:
<?php
$ascii_val = ord("a");
for($i=$ascii_val;$i<$ascii_val+26;$i++){
echo chr($i)."\n";
}
?>
where 26 is the total number of letters in the alphabet.
There are several ways to do that.
1. First you can take the range from 'a' to 'z'. Then iterate a loop over it.
foreach(range('a', 'z') as $i)
{
echo $i . "\n";
}
2. You can print the letters using asci value of the characters.
for($i = 97 ; $i<=122; $i++)
{
echo chr($i) . "\n";
}
3. You can take the asci value of 'a' and run a loop till the asci value of 'z':
for ($x = ord('a'); $x <= ord('z'); $x++)
{
echo chr($x) . "\n";
}
4. For printing a new line in html you can append the to the end of each characters.
for($i='a';$i<='z';$i++)
{
echo $i. "<br />";
}
this code will work. It’s easy & can be understood:
<?php
// print form A to ZZ like this
// A B C ... AA AB AC ... ZA .. ZZ
$pre = "";
$i = ord('a');
for ($x = ord('a'); $pre . strtoupper(chr($x-1)) != 'AH'; $x++)
{
echo "". $pre . strtoupper(chr($x)) . "\n";
if(chr($x) === "z"){
$pre = strtoupper(chr($i++));
$x = ord('a');
$x--;
}
}
One more way to get the display of characters a-z:
<?php
for ($i = 'a'; $i < 'z'; $i++){
echo "$i\n";
}
echo $i; // 'z'
See live code
Once the conditional loop becomes false, $i will have already been incremented and have the string value corresponding to 'z' which can be displayed.
Wow I really didn't know about this but its not a big code you can try echo "z" after loop Mark is Absolutely Right I use his method but if you want alternative then this may also you can try
<?php
for ($i = "a"; $i = "y"; $i++) {
echo "$i\n";
if ($i == "z") {}
}
echo "z";
?>

Categories