And here's a good example of why you should always test each solution and time it to see what's better. I was recommending a preg_replace_callback solution which I thought, as a lot of other people would also think, is a lot faster that your own "method" of doing it. So, I wrote the preg_replace_callback method and timed it against your method using the following input and replacement strings:
//test string $string = "This is a [test] of something [that] will hopefully [work]."; //replacement strings $test = "test_one, test_two [foo], test_three"; $that = "that_one, that_two, that_three [foo]"; $work = "work_one [foo], work_two, work_three"; $foo = "foo_one [test], foo_two [that], foo_three [work]"; That's actually some complex replacement because [foo] can be replaced with a replacement that refers back to [test] which refers again to [foo], etc... So actually each run will result in a different string entirely and would take different amounts of times based on what was randomly chosen as a replacement. So, I ran each method 1000 times to kind of equal out the randomness... Anyway, here are the results. The test file I used is also attached. You may be able to tweak it further, I don't know. But, since you're opening and reading files, the small difference between the two may be negated by having to deal with files... always test your solutions. Original String: This is a [test] of something [that] will hopefully [work]. Replaced String: This is a test_two foo_one test_two foo_two that_one of something that_two will hopefully work_three. Preg_replace_callback Time: 0.226889014244 ------------------------------------------------------------------------ ---- Original String: This is a [test] of something [that] will hopefully [work]. Replaced String: This is a test_two foo_one test_three of something that_two will hopefully work_two. String-parser Time: 0.187005996704 (The "replaced string" shown is just the last result of running it 1000 times) ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ > -----Original Message----- > From: Steve Keller [mailto:[EMAIL PROTECTED]] > Sent: Thursday, December 12, 2002 5:09 PM > To: [EMAIL PROTECTED] > Subject: RE: [PHP] Odd Strpos Behavior > > 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: [EMAIL PROTECTED] > Web: www.healthtvchannel.org > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php