Odd Strpos Behavior

From: Date: Mon, 09 Dec 2002 23:38:55 +0000
Subject: Odd Strpos Behavior
Groups: php.general 
Request: Send a blank email to php-general+get-127570@lists.php.net to get a copy of this message
I'm getting a really weird return from Strpos. What I'm doing is this, and anyone familiar with any of the table-runner programs for RPG's will know what I'm getting at here, I have a fields, like [adjective], [noun], etc., which I need to pull out and replace with values from included php files. Ok, so I have a string with those fields in it, and I have a loop that scans through the string for the first occurence of ]. On the first pass, strpos returns the first instance of where ] shows up in the string. Beautiful. I cut out the field, I create a filename, and it works just fine. However, on the second run through the loop, instead of returning where ] is in the string, it returns where ] is +1. Huh? It's throwing off my script. Obviously, I could compensate by keeping a counter, but really, I shouldn't have to. Anyone know why I'm getting this? Here's the code. I echoed the values so I could see what was going wrong. <? $a = "[adjective] [beginning]"; $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"; $tmp .= $dataFile; $tmp .= $a; $a = $tmp; $e = strpos($a,"]"); } # echo $a; ?> This is running on Apache, php4.2.3 -- 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 (#127570) next »