RE: [PHP] Odd Strpos Behavior

From: Date: Thu, 12 Dec 2002 22:09:27 +0000
Subject: RE: [PHP] Odd Strpos Behavior
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-128083@lists.php.net to get a copy of this message
At 12/11/2002 08:09 PM, you wrote:
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. -- S. Keller UI Engineer The Health TV Channel, Inc. (a non - profit organization) 3820 Lake Otis Pkwy. Anchorage, AK 99508 907.770.6200 ext.220 907.336.6205 (fax) Email: skeller@healthtvchannel.org Web: www.healthtvchannel.org

« previous php.general (#128083) next »