0

I want make from word "dsafasd".

d
s
a
f
ds
sd
dsa
asd
dsaf
fsad
dsafa
afasd
etc.. 

Until last word is "dsafasd".

So, the sequence will be like this in string position

0
1
2
3
01
65
012
654
0123
6543
01234
65432
etc..

Until last word is 0123456 in string location.

At first phase, it will print 1 letter from most left to middle.
At second phase, it will print 2 letters from left and right.
At third phase, it will print 3 letters from left and right.
This phase will continue until the result print the same word as word was inputted.

My Problem is I don't know to make increment from f to ds, sd to dsa and so on. I know i need 1 or more for for length inside substr($word,$i,1) and 2 or more if for identify I need to stop in that length, move on to length+1 and print them in back and forth but I don't know how the criterias or the logics inside it.

Here's some thought in my mind:

$a = "dsafasd";
$b = strlen($a);
$c = ($lengthWord+1)/2;

$i=0;
$h=1;
do {
do {
echo substr($a,$i,$h),"<br>";
$i++;
} while ($i!=$c);#First Phase ends here
$h++;
$i=0;
echo substr($a,$i,$h),"<br>";
echo strrev(substr($a,$i,$h)),"<br>";
} while ($h<!=$b);

And the output becomes:

d
s
a
f
ds
sd
ds <--
sa <--
af <--
fa <--
dsa
asd
dsa <--
saf <--
afa <--
fas <--
dsaf
fasd
dsaf <--
safa <--
afas <--
fasd <--
dsafa
afasd
dsafa <--
safas <--
afasd <--
fasd <--
dsafas
safasd
dsafas <--
safasd <--
afasd <--
fasd <--
dsafasd
dsafasd <--

I can't make it stop after 2nd word was deployed and double word at last sequence. Which make words on my marks <-- shouldn't exist.

Nanoticon
  • 9
  • 3
  • Does this answer your question? [Get all possible substring in php](https://stackoverflow.com/questions/38168248/get-all-possible-substring-in-php) – Rahul Nov 07 '19 at 10:06
  • @Rahul it doesn't work, the result isn't fit order that I was explained. – Nanoticon Nov 07 '19 at 10:46

1 Answers1

0

Here's the solution I came up with using nested loops. See code, output and explanation below.

The code:

$str = "dsafasd";
$length = strlen($str);

for($i=1; $i<=$length; $i++) {
    if($i==1) {
        for($x=0; $x<$length; $x++) {
            echo substr($str,$x,$i)."<br>"; 
            if($x == floor($length/2)) break;
        }
    } else {
        $subStr = substr($str,0,$i);
        echo $subStr."<br>";
        if($subStr == $str) break;
        echo strrev($subStr)."<br>";
    }
}

Code Version 2:

for($i=1; $i<=$length; $i++) {
    if($i==1) {
        for($x=0; $x<$length; $x++) {
            echo substr($str,$x,$i)."<br>"; 
            if($x == floor($length/2)) break;
        }
    } else {
        $subStr = substr($str,0,$i);
        echo $subStr."<br>";
        if($subStr == $str) break;
        $offset = $length-$i;
        echo substr($str,$offset,$i)."<br>";
    }
}

The output:

d
s
a
f
ds
sd
dsa
asd
dsaf
fasd
dsafa
afasd
dsafas
safasd
dsafasd

Explanation:

The outer loop creates a variable $i that represents the length or the substring that will be created.

If the ouptput string is 1 character it will run the inner loop. This creates a variable $x that represents the start position of the substring. The conditional breaks the loop when it reaches half the string as you asked.

In the else statement (which means the output string is more than 1 char) we echo the first n charachters where n = $i. Then we echo the reverse of that value.

Finally, theres a break clause that will stop the loop once you output "dsafasd".

Code Version 2 does the same thing but doesn't reverse the output. Instead, it gets the last n letters of the string. Outputs the same result.

Hope this is what you were looking for.

  • Thank you for replying! It wasn't quite that I want but you did give me inspiration about another thought that lead me to code I want. *Note I updated my post for clearer informations. – Nanoticon Nov 07 '19 at 14:39
  • I think you have to clarify your question. According to your example, after single letters, your expected output is "ds" then "sd". You said this is equivalent to "12" and "76". But the actual output of "12" and "76" is "ds" and "ds". Do you mean "12" and "67"? – Paolo Rodriguez Nov 08 '19 at 05:24
  • Edited my answer. It now produces the output you want but not the method you prescribed. Like I said, you need to clarify your question. – Paolo Rodriguez Nov 08 '19 at 05:35
  • Thank you! You solved my problem. And I corrected my example from String Positioning after you mentioned it. – Nanoticon Nov 08 '19 at 07:24
  • Glad I could help. – Paolo Rodriguez Nov 08 '19 at 07:29