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 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()函数使用的更多相关文章
- mt_rand()函数、str_shuffle() 函数、join() 函数
mt_rand() 使用 Mersenne Twister 算法返回随机整数. 语法 mt_rand(min,max) 定义和用法 str_shuffle() 函数随机地打乱字符串中的所有字符. 语法 ...
- php str_shuffle()函数 语法
php str_shuffle()函数 语法 str_shuffle()函数怎么用? php str_shuffle()函数用于随机的打乱字符串中所有字符,语法是str_shuffle(string) ...
- PHP str_shuffle() 函数
实例 随机地打乱字符串中的所有字符: <?php高佣联盟 www.cgewang.comecho str_shuffle("Hello World");?> 定义和用法 ...
- OC与c混编实现Java的String的hashcode()函数
首先,我不愿意大家需要用到这篇文章里的代码,因为基本上你就是被坑了. 起因:我被Java后台人员坑了一把,他们要对请求的参数增加一个额外的字段,字段的用途是来校验其余的参数是否再传递过程中被篡改或因为 ...
- string类find函数返回值判定
string类find函数返回值判定 代码示例 #include<iostream> #include<cstring> using namespace std; int m ...
- C string.h 常用函数
参考:http://womendu.iteye.com/blog/1218155 http://blog.csdn.net/zccst/article/details/4294565 还有一些,忘记了 ...
- c++中string的常用函数说明
string可以说是是字符数组的升级版,使用更加啊方便,不容易出错.本文对string的常用函数进行简单介绍,做到会用即可. string中的常用函数分为四类,即赋值,添加,比较和删除. 一.赋值 1 ...
- C++ string类及其函数的讲解
文章来源于:http://www.cnblogs.com/hailexuexi/archive/2012/02/01/2334183.html C++中string是标准库中一种容器,相当于保存元素类 ...
- PHP之string之explode()函数使用
explode (PHP 4, PHP 5, PHP 7) explode - Split a string by string explode - 使用一个字符串分割另一个字符串 Descripti ...
随机推荐
- java中的四种代码块
一.普通代码块 直接在一个方法中出现的{}就称为普通代码块,例子程序如下: public class CodeDemo01{ public static void main(String[] args ...
- Lucene--FuzzyQuery与WildCardQuery(通配符)
FuzzyQuery: 创建索引: IndexWriter writer = new IndexWriter(path, new StandardAnalyzer(), false); writer. ...
- 不用外部插件启用u盘ntfs写功能
mac下启用NTFS u盘读写功能. 不用要任何外部插件,其实mac本来就支持,只是因为专利原因隐藏了而已. macbook:~ uwe$ sudo umount /Volumes/UNTITLED ...
- 部署网络存储ISCSI
1.什么是ISCSIInternet Small Computer System Interface 互联网小型计算机接口技术,是一种将SCS存储与以太网技术相结合,可以用来在互联网中传输SCSI接口 ...
- ceph 移除 osd
1:从crush中移除节点ceph osd crush remove osd.0 2:删除节点ceph osd rm osd.0 3:删除节点认证(不删除编号会占住)ceph auth del osd ...
- 201621123023《Java程序设计》第12周学习总结
一.本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 二.书面作业 本次PTA作业题集多线程 1. 面向系统综合设计-图书馆管理系统或购物车 使用流与文件改造你的图书 ...
- opencv3.3 安装环境教程以及实现个图片读取功能
一.opencv3.3安装环境 1. 首先要安装这个opencv3.3,我们必须要安装一个python (IDLE:集成开发环境),我自己安装了IDLE3.6和pycharm2017,这些软件都可以去 ...
- CF1110G Tree-Tac-Toe(博弈论)
题面 传送门 题解 博弈论的前提是双方都是绝顶聪明的人 所以这种题目显然不是我应该做的 题解 //minamoto #include<bits/stdc++.h> #define R re ...
- 详解sizeof与strlen
一,sizeof是C语言的一种单目运算符,与C语言的其他运算符++,--一样,它并不是函数:sizeof()以字节为单位给出了操作数的大小:sizeof的值是无符号int. strlen是一个函数,只 ...
- 如何到python模块路径linux
执行命令whereis python即可显示出python相关的所有的路径,包括可执行文件路径,安装路径等,该方法适用于大部分类似的场景抄自百度知道