Re: str_replace

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

 



On Sun, Apr 24, 2011 at 11: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 );

    I just mocked this up now, and only tested it twice.  It's not the
most elegant solution, and probably shouldn't be used in high-demand
situations, but it should at least serve to get you started.

<?php

$str = '%ECARBME%TIPLUP%%%%%%%E%%';
$chars = 'abcdefghijklmnopqrstuvwxyz';

echo multi_replace($str,'%',$chars).PHP_EOL; // Case-sensitive,
random, straight replace
echo multi_replace($str,'%',$chars,0,1,1).PHP_EOL; //
Case-insensitive, random, randomg casing

function multi_replace($str,$targ,$chars,$sens=true,$random=true,$caserand=false)
{

        // Loop through while $targ is still found in $str
        while (strstr($str,$targ) !== false) {

                // If we're randomizing, pick the character; else the
first (or only)
                if ($random == true && !is_null($random)) {
                        $replace = $chars[mt_rand(0,(strlen($chars) - 1))];
                } else {
                        $replace = substr($chars,0,1);
                }

                // If we want random casing, do that; else make no change
                if ($caserand == true && !is_null($caserand)) {
                        if (mt_rand(0,1) === 0) {
                                $replace = strtolower($replace);
                        } else {
                                $replace = strtoupper($replace);
                        }
                }

                // If we don't want case sensitivity, set the regexp modifier
                $mod = $sens == false || is_null($sens) ? 'i' : '';

                // Perform the match and replace only one character now
                $str = preg_replace('/'.$targ.'/U'.$mod,$replace,$str,1);

        } // End of loop

        // Return the modified string
        return $str;
}
?>

-- 
</Daniel P. Brown>
Network Infrastructure Manager
http://www.php.net/

-- 
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