RE: [PHP] Odd Strpos Behavior

From: Date: Fri, 13 Dec 2002 12:43:44 +0000
Subject: RE: [PHP] Odd Strpos Behavior
Groups: php.general 
Request: Send a blank email to php-general+get-128152@lists.php.net to get a copy of this message
> -----Original Message----- > From: Steve Keller [mailto:skeller@healthtvchannel.org] > Sent: 12 December 2002 22:09 > > 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]. Well, I think I'd approach this using preg_match_all() to break out all your elements for replacement into an array, then look them up and produce a second array of the desired replacements, and then do a preg_replace using those two arrays to do your desired replaces. Something like (completely untested!): $matches = preg_match_all('|\[(.*\)]|U', $sentence, $items); if ($matches): $replacements = array(); foreach ($items[1] as $i=>$item): $replacements[$i] = /* random item looked up in "$item.php" */; $items[1][$i] = '|\['.$item.'\]|'; // reconstruct as valid preg pattern endforeach; $new_sentence = preg_replace($items[1], $replacements, $sentence); endif; Cheers! Mike --------------------------------------------------------------------- Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: m.ford@lmu.ac.uk Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211

« previous php.general (#128152) next »