# Bit of help with a string function?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hi all,
Hoping for a simplification (and fix!) of this bit of code....
The idea is that it takes a long string formatted with a tree-
structure and splits it into a list of variables that each know their
parent section.... easier to understand by running the code:

\$parent='root';

//prepare vars:
\$newpages="section 1\nsection 2\nsection 3\n*section 3b\n**section 3b
part(i)\n**section 3b part(ii)\n*section 3c\nsection 4\nsection 5";
\$newpages=explode("\n",\$newpages);
\$lastpage=\$parent;
\$error=false;
\$stars=0;
\$parentsArray=array(\$parent);
foreach(\$newpages as \$newpage){
\$i=0;
\$thispage=explode("*",\$newpage);
//print_r(\$thispage);echo "<br />";
while(!\$thispage[\$i]){
\$i++;
if(!\$parentsArray[\$i])
\$stars=\$i;
}
while(\$stars>\$i){
\$stars-=1;
array_pop(\$parentsArray);array_pop(\$parentsArray);
}
echo "<strong>".\$thispage[\$i]."</strong> is a subsection of
<strong>".end(\$parentsArray)."</strong><br />";
\$lastpage=\$thispage[\$i];
}

//////////////////////
/* OUTPUTS:

section 1 is a subsection of root
section 2 is a subsection of root
section 3 is a subsection of root
section 3b is a subsection of section 3
section 3b part(i) is a subsection of section 3b
section 3b part(ii) is a subsection of section 3b
section 3c is a subsection of section 3b
section 4 is a subsection of root
section 5 is a subsection of root

*/

## Re: Bit of help with a string function?

Ciaran wrote:

So, if the output is correct, then what is the problem? error messages?

## Re: Bit of help with a string function?

OK I've managed to get it to work correctly. I have a feeling it could
be simplified tho so if anyone has any ideas, please fire them over!

Here's the working code:

//input:
\$newpages="section 1\nsection 2\nsection 3\n*section 3b\n**section 3b
part(i)\n**section 3b part(ii)\n*section 3c\nsection 4\nsection 5\n*5b
\n*5c\n**5ci\n**5cii\n***5ciiA\n*5d\n**5di\n6";
echo str_replace("\n","<br />",\$newpages)."<br /><br />";

//setup vars:
\$parent='root';
\$newpages=explode("\n",\$newpages); //split by line break
\$lastpage=\$parent;
\$stars=0;
\$parentsArray=array(\$parent);

//run loop:
foreach(\$newpages as \$newpage){
\$i=0;
\$thispage=explode("*",\$newpage); //split each line by stars (if any)
while(!\$thispage[\$i]){//Each time a star is found at the start of the
line
\$i++;
if(!\$parentsArray[\$i])//store the
}
\$stars=\$i;
while(\$stars<(count(\$parentsArray)-1)){ //if there are less stars on
this line than the previous line, remove the newest parent
array_pop(\$parentsArray);
}

echo "<strong>".\$thispage[\$i]."</strong> is a subsection of
<strong>".end(\$parentsArray)."</strong><br />";
\$lastpage=\$thispage[\$i];
}

OUTPUTS THE FULL LIST FIRST:
section 1
section 2
section 3
*section 3b
**section 3b part(i)
**section 3b part(ii)
*section 3c
section 4
section 5
*5b
*5c
**5ci
**5cii
***5ciiA
*5d
**5di
6

THEN OUTPUTS PARENT/CHILD PAIRS:
section 1 is a subsection of root
section 2 is a subsection of root
section 3 is a subsection of root
section 3b is a subsection of section 3
section 3b part(i) is a subsection of section 3b
section 3b part(ii) is a subsection of section 3b
section 3c is a subsection of section 3
section 4 is a subsection of root
section 5 is a subsection of root
5b is a subsection of section 5
5c is a subsection of section 5
5ci is a subsection of 5c
5cii is a subsection of 5c
5ciiA is a subsection of 5cii
5d is a subsection of section 5
5di is a subsection of 5d
6 is a subsection of root