Home  >  Article  >  Backend Development  >  PHP's str_shuffle() function randomly shuffles all characters in a string

PHP's str_shuffle() function randomly shuffles all characters in a string

黄舟
黄舟Original
2017-11-03 13:29:532595browse

Example

Randomly shuffle all characters in string:

<?php
echo str_shuffle("Hello World");
?>

Definition and usage

str_shuffle() Function Randomly shuffle all characters in the string.

Syntax

str_shuffle(string)
Parameters Description
string Required. Specifies the string to be scrambled.

Technical details

Return value: Returns the scrambled string.
PHP version: 4.3.0+

str_shuffle(): Randomly shuffle the string order.

You can generate a different string every time by combining the str_shuffle() function and the substr() function.

The following are two examples of the str_shuffle() function:

Example 1: Randomly generate a string of 10 digits in length.

$str="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
str_shuffle($str);
$name=substr(str_shuffle($str),26,10);
echo $name;

Running result:

bdgNIC04wF

Example 2: A 10-digit string starting with NT is generated.

$str="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
$str=&#39;NT&#39;.substr(str_shuffle($str),5,8);
echo $str;

Run result:

NTZYwKiDaF

The above is the detailed content of PHP's str_shuffle() function randomly shuffles all characters in a string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn