On Sun, Apr 24, 2011 at 11:44 AM, Ron Piggott < ron.pigg...@actsministries.org> wrote:
> > I am trying to figure out a syntax that will replace each instance of % > with a different letter chosen randomly from the string $puzzle_filler. > $puzzle_filler is populated with the letters of the alphabet, roughly in the > same ratio as they are used. > > This syntax replaces each instance of % with the same letter: > > $puzzle[$i] = str_replace ( "%" , ( substr ( $puzzle_filler , rand(1,98) , > 1 ) ) , $puzzle[$i] ); > > Turning this: > > %ECARBME%TIPLUP%%%%%%%E%% > > Into: > > uECARBMEuTIPLUPuuuuuuuEuu > > Is there a way to tweak my str_replace so it will only do 1 % at a time, so > a different replacement letter is selected? > > This is the syntax specific to choosing a replacement letter at random: > > substr ( $puzzle_filler , rand(1,98) , 1 ); > > Thanks for your help. > > Ron > > The Verse of the Day > “Encouragement from God’s Word” > http://www.TheVerseOfTheDay.info > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > Quick guess trying to be fairly performant: function fill_puzzle($puzzle, $puzzle_filler) { $puzzle_length = strlen($puzzle); $puzzle_filler_length = strlen($puzzle_filler); for ($i = 0; $i < $puzzle_length; $i++) { if ($puzzle[$i] == '%') { $puzzle[$i] = $puzzle_filler[mt_rand(0, ($puzzle_filler_length - 1))]; } } return $puzzle; } echo fill_puzzle($puzzle = "%ECARBME%TIPLUP%%%%%%%E%%", $puzzle_filler = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); Happy Easter :) Adam -- Nephtali: A simple, flexible, fast, and security-focused PHP framework http://nephtaliproject.com