PHP之string之str_shuffle()函数使用

发布时间:2019-08-07 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP之string之str_shuffle()函数使用脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

str_shuffle

  • (PHP 4 >= 4.3.0, PHP 5, PHP 7)
  • str_shuffle — RandoMLy shuffles a string
  • str_shuffle — 随机打乱一个字符串

Description

string str_shuffle ( string $str )
//str_shuffle() shuffles a string. One PErmutation of all possible is created.
//str_shuffle() 函数打乱一个字符串,使用任何一种可能的排序方案

Caution

  • This function does not generate cryptograph@R_512_2411@ly secure values, and should not be used for cryptographic purposes. If you need a cryptographically secure value, consider using random_int(), random_bytes(), or openssl_random_pseudo_bytes() instead.
  • 本函数并不会生成安全加密的值,不应用于加密用途。若需要安全加密的值,考虑使用Openssl_random_pseudo_bytes()。

Parameters

str

  • The input string.
  • 输入字符串。

Return Values

  • Returns the shuffled string.
  • 返回打乱后的字符串。

ChangeLOG

  • 7.1.0 The internal randomization algorIThm has been changed to use the » Mersenne Twister Random Number Generator instead of the libc rand function.
  • 内置的随机算法从 libc rand 函数改成了» 梅森旋转演伪随机数发生算法。

Examples

<?php
/**
 * Created by PhpStorm.
 * User: zhangrongxiang
 * Date: 2018/3/7
 * Time: 下午10:04
 */

$str = "abcde";
echo str_shuffle( $str ) . PHP_EOL;
echo str_shuffle( $str ) . PHP_EOL;
echo str_shuffle( $str ) . PHP_EOL;
echo str_shuffle( $str ) . PHP_EOL;

/////////////////////////////////////////////////////////////////////////////////
function scramble_word( $word ) {
    if ( strlen( $word ) < 2 ) {
        return $word;
    } else {
        return $word{0} . str_shuffle( substr( $word, 1, - 1 ) ) . $word{strlen( $word ) - 1};
    }
}

//echo preg_replace('/(w+)/e', 'scramble_word("1")', 'A quick brown fox jumped over the lazy dog.');
echo scramble_word( "A quick brown fox jumped over the lazy dog." ) . PHP_EOL;

///////////////////////////////////////////////////////////////////////////////
/**This function is affected by srand():*/
srand( 12345 );
echo str_shuffle( 'Randomize me' ) . PHP_EOL; // demmiezR aon
echo str_shuffle( 'Randomize me' ) . PHP_EOL; //izadmeo Rmen

srand( 12345 );
echo str_shuffle( 'Randomize me' ) . PHP_EOL; // demmiezR aon

////////////////////////////////////////////////////////////////////////////
// not very true
srand(time());
function unicode_shuffle( $string, $chars, $format = 'UTF-8' ) {
    $rands = [];
    for ( $i = 0; $i < $chars; $i ++ ) {
        $rands[ $i ] = rand( 0, mb_strlen( $string, $format ) );
    }
    $s = null;
    foreach ( $rands as $r ) {
        $s .= mb_substr( $string, $r, 1, $format );
    }
    
    return $s;
}

echo unicode_shuffle( "万物结对象", 3 ) . PHP_EOL;

See

All rights reserved

脚本宝典总结

以上是脚本宝典为你收集整理的PHP之string之str_shuffle()函数使用全部内容,希望文章能够帮你解决PHP之string之str_shuffle()函数使用所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。