Re: str_replace

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 24 April 2011 16:44, Ron Piggott <ron.piggott@xxxxxxxxxxxxxxxxxx> 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
>
>

Something like this ...

<?php
$s_Text = '%ECARBME%TIPLUP%%%%%%%E%%';
$s_PuzzleFilter = '0123456789';
echo preg_replace_callback(
  '`%`',
  function() use($s_PuzzleFilter) {
    return substr($s_PuzzleFilter, mt_rand(0, strlen($s_PuzzleFilter)
- 1), 1);},
  $s_Text
);


-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux