百科狗-知识改变命运!
--

str_shuffle() - 随机打乱一个字符串 - php 字符串函数

乐乐1年前 (2023-11-21)阅读数 19#技术干货
文章标签字符串

str_shuffle()

(PHP 4 >= 4.3.0, PHP 5, PHP 7)

随机打乱一个字符串

说明

str_shuffle(string $str) : string

str_shuffle()函数打乱一个字符串,使用任何一种可能的排序方案。

Caution

本函数并不会生成安全加密的值,不应用于加密用途。若需要安全加密的值,考虑使用openssl_random_pseudo_bytes()。

参数

$str

输入字符串。

返回值

str_shuffle() - 随机打乱一个字符串 - php 字符串函数

返回打乱后的字符串。

更新日志

版本说明
7.1.0内置的随机算法从 libc rand 函数改成了»梅森旋转演伪随机数发生算法。

范例

Example #1str_shuffle()范例

参见

  • shuffle()打乱数组
  • rand()产生一个随机整数
Aoccdrnig to rseearch at an Elingsh uinervtisy, it deosn't mttaer in waht oredr the ltteers in a wrod are, the olny iprmoatnt tihng is that the frist and lsat ltteer is at the rghit pclae. The rset can be a toatl mses and you can sitll raed it wouthit a porbelm. Tihs is bcuseae we do not raed ervey lteter by it slef but the wrod as a wlohe.
Hree's a cdoe taht slerbmcas txet in tihs way:

It may be ufseul if you wnat to cetare an aessblicce CTCPAHA.
This function is affected by srand():
A proper unicode string shuffle;

$str = "Şeker yârim"; // My sweet love
echo str_shuffle($str); // ieymrŢekr 
echo str_shuffle_unicode($str); // Şr mreyeikâ
This page is missing a very important notice:
Caution
This function does not generate cryptographically 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.
Shuffle for all encoding formats
/**
 * Test shuffleString
 */
function testShuffleString() {
  $shuffled = shuffleString("ĄęźćÓ");
  if (\mb_strlen($shuffled) != 5) {
    throw new \UnexpectedValueException("Invalid count of characters");
  }
  if ($shuffled == "ĄęźćÓ") {
    throw new \UnexpectedValueException("The same string");
  }
  foreach (["Ą", "ę", "ź", "ć", "Ó"] as $char) {
    if (\mb_strpos($shuffled, $char) === false) {
      throw new \UnexpectedValueException("Character not found");
    }
  }
}
/**
 * Shuffle string
 *
 * @param $stringValue String to shuffle
 * @param string $startWith Shuffle $stringValue and append to $startWith
 * @return string Shuffled string
 * @author Krzysztof Piasecki
 */
function shuffleString($stringValue, $startWith = "") {
  $range = \range(0, \mb_strlen($stringValue));
  shuffle($range);
  foreach($range as $index) {
    $startWith .= \mb_substr($stringValue, $index, 1);
  }
  return $startWith;
};
testShuffleString();
echo shuffleString("Hello"); // > 'elHol' (something like this)
echo shuffleString("World!", "Hello "); // > 'Hello do!lrW' (something like this)
As noted in this documentation str_shuffle is NOT cryptographically secure, however I have seen many code examples online of people using nothing more than this to generate say random passwords. So I though I'd share my function which while it makes use of str_shuffle also rely's on random_int() for added security. I use this function to generate salts to use when working with hashes but it can also be used to generate default passwords for new users for example.
It starts with a universe of possible characters, in this case all letters (upper and lower case), 0-9, and several special characters.
It then will run str_shuffle on the universe of characters a random number of times, using random_int() (currently set to 1-10)
Then once the universe of possible characters has been shuffled it using random_int() once more to select the character as a random position within the shuffled string, as does that once for each character you want in the output.
 function secret_gen( $len=64 ) {
  $secret = "";
  $charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_-+=`~,.[]:  | ';
  for ( $x = 1l $x 

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)