简体   繁体   中英

PHP text shuffling and all possible options

Say I have a text string: {a|b|c|d} {a|b|c|d} {a|b|c|d} {a|b|c|d} .

And simple PHP function to shuffle the text:

function fb_filter_shuffle($string)
{
    if(empty($string))
    {
        return NULL;
    }

    return preg_replace_callback('/\{.*?\}/i', function($m)
    {
        $options    = explode('|', mb_substr($m[0], 1, -1));

        shuffle($options);

        return current($options);
    }, $string);
}

I need to get all possible variations of the output. But how? the only option I came up so far, was to run the code nnn times and pick the unique options. Any more efficient suggestions?

30 seconds with Google and look what I came up with ;)

http://docstore.mik.ua/orelly/webprog/pcook/ch04_25.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM