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

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

黄舟
Release: 2023-03-17 06:08:01
Original
2643 people have browsed it

Example

Randomly shuffle all characters in string:

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

Definition and usage

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

Syntax

str_shuffle(string)
Copy after login
ParametersDescription
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;
Copy after login

Running result:

bdgNIC04wF
Copy after login

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;
Copy after login

Run result:

NTZYwKiDaF
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template