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 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.
  • 本函数并不会生成安全加密的值,不应用于加密用途。若需要安全加密的值,考虑使用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()函数使用的更多相关文章

  1. mt_rand()函数、str_shuffle() 函数、join() 函数

    mt_rand() 使用 Mersenne Twister 算法返回随机整数. 语法 mt_rand(min,max) 定义和用法 str_shuffle() 函数随机地打乱字符串中的所有字符. 语法 ...

  2. php str_shuffle()函数 语法

    php str_shuffle()函数 语法 str_shuffle()函数怎么用? php str_shuffle()函数用于随机的打乱字符串中所有字符,语法是str_shuffle(string) ...

  3. PHP str_shuffle() 函数

    实例 随机地打乱字符串中的所有字符: <?php高佣联盟 www.cgewang.comecho str_shuffle("Hello World");?> 定义和用法 ...

  4. OC与c混编实现Java的String的hashcode()函数

    首先,我不愿意大家需要用到这篇文章里的代码,因为基本上你就是被坑了. 起因:我被Java后台人员坑了一把,他们要对请求的参数增加一个额外的字段,字段的用途是来校验其余的参数是否再传递过程中被篡改或因为 ...

  5. string类find函数返回值判定

     string类find函数返回值判定 代码示例 #include<iostream> #include<cstring> using namespace std; int m ...

  6. C string.h 常用函数

    参考:http://womendu.iteye.com/blog/1218155 http://blog.csdn.net/zccst/article/details/4294565 还有一些,忘记了 ...

  7. c++中string的常用函数说明

    string可以说是是字符数组的升级版,使用更加啊方便,不容易出错.本文对string的常用函数进行简单介绍,做到会用即可. string中的常用函数分为四类,即赋值,添加,比较和删除. 一.赋值 1 ...

  8. C++ string类及其函数的讲解

    文章来源于:http://www.cnblogs.com/hailexuexi/archive/2012/02/01/2334183.html C++中string是标准库中一种容器,相当于保存元素类 ...

  9. PHP之string之explode()函数使用

    explode (PHP 4, PHP 5, PHP 7) explode - Split a string by string explode - 使用一个字符串分割另一个字符串 Descripti ...

随机推荐

  1. vs的 Avalon 自动补全

    以VS2013为例: 1.关闭 Visual Studio 2.打开 C:/Program Files (x86)/Microsoft Visual Studio 12.0/Common7/Packa ...

  2. 更改kvm虚拟机磁盘大小

    kvm 虚拟机的磁盘大小可通过命令:qemu-img resize filename size 来改,要注意的是resize只支持raw格式的磁盘文件,如果想更改qcow2等格式的磁盘大小,需先用qe ...

  3. SQL 判断数据库是否有相关表 字段

    --判断数据库是否有相关表 if exists (select 1 from sysobjects where id = object_id(' 表名 ') and type = ' U ' ); - ...

  4. HBase - 数据写入流程解析

    本文由  网易云发布. 作者:范欣欣 本篇文章仅限内部分享,如需转载,请联系网易获取授权. 众所周知,HBase默认适用于写多读少的应用,正是依赖于它相当出色的写入性能:一个100台RS的集群可以轻松 ...

  5. django系列8.1--django的中间件01 自定义中间件的5个方法

    一.Django中的中间件 Django中间件定义: Middleware is a framework of hooks into Django's request/response process ...

  6. Oracle数据库exp和imp方式导数据

    这里导入导出路径都在D盘下,默认文件名为:example.dmpexp方式导出数据相关参数项如下: 关键字  说明  默认USERID                     用户名/口令FULL   ...

  7. 【ocp-12c】最新Oracle OCP-071考试题库(39题)

    39.choose the best answer View the Exhibit and examine the description of the EMPLOYEES table. You w ...

  8. “全栈2019”Java异常第三章:try代码块作用域详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...

  9. 3D-2D:PnP

    PnP(Perspective-n-Point):当知道n个3D空间点及其投影位置时,估计相机位姿. 2D-2D的对极几何方法需要八个或八个以上的点对(以八点法为例),且存在着初始化.纯旋转和尺度的问 ...

  10. robot framework学习笔记之十一--第三方库requests详解

    一.安装 Requests 通过pip安装 pip install requests 或者,下载代码后安装: $ git clone git://github.com/kennethreitz/req ...