> >Okay, so how do you know what to replace something like [author]
with?
> >What exactly are you doing again? I've forgotten the original
question.
> >:)
>
> Ok, got a sentence, like:
>
> a pile of [metal] 600 feet wide and 30 feet tall. On top of
it is
> a [monster].
>
> The items in the [] represent the names of files containing lists of
> possible results. So I need to grab the first one, "metal," open up
> "metal.php," grab a random item from it, such as "gold," and replace
> [metal] in the original sentence with the result. I should now have:
>
> a pile of gold 600 feet wide and 30 feet tall. On top of it
is a
> [monster].
>
> Now, what I was doing before was strpos'ing the []'s and grabbing what
as
> in between to be the file name, then repeating in a loop until I've
> eliminated all of the [] items. The reason I'm doing it in a loop is
> because the results of the "metal.php" random item may include their
own
> []
> items, which also need to be run exactly the same way. A result might
be
> "gold [objects]" and then I have to run through the objects.php file
and
> get a result from that.
>
> This is what I started with:
>
> <?
> $a = "[adjective] [beginning]"; // temporary item for testing
>
> $e = strpos($a,"]");
> while ($e) {
> echo "A: ".$a."<br />";
> echo "E: ".$e."<br />";
> $f = strpos($a, "[");
> echo "F: ".$f."<br />";
> $tmp = substr($a, 0, $f);
> $table=substr($a, $f+1, $e-1);
> echo "Table: ".$table."<br />";
> $a = substr($a, $e+1, strlen($a));
> $dataFile = $table.".php";
> //$b = getFileElement($dataFile);
> $tmp .= $b;
> $tmp .= $a;
> $a = $tmp;
> $e = strpos($a,"]");
> }
> echo $a;
> ?>
>
> That should work just fine. I cut out everything up to the first [ and
add
> it to $tmp. Then I get the next sequence of characters up to the first
]
> and use it as a file name for the "getFileElement" function. Add the
> result
> to $tmp, add what was left after the first ], and viola.
>
> My problem is that, on the first loop through, strpos returns exactly
> where
> the first ] is, so I can chop up to that no problem. However, the
second
> time through the loop, it's one off, which breaks the logic of the
loop, I
> end up with "beginning]" as my file name.
Here's your fix for the method you are using now, btw:
Change:
$table=substr($a, $f+1, $e-1);
to:
$table=substr($a, $f+1, $e-$f-1);
$f is position of first [, $e is position of first ]. So, you don't want
a substr length of $e-1 (which is from the beginning of the string, you
want the length to be the difference of $f from $e. Hope that helps.
Your original code was only working for the first one because you
started of with a [word]. If you put text before the first [word], it
fails on all accounts.
---John W. Holmes...
PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php