大网数据广告活动
PHP 语言与运行手册

Random\Randomizer::shuffleBytes · Get a byte-wise permutation of a string

· 52CE 我爱测

Random\Randomizer::shuffleBytes — Get a byte-wise permutation of a string

Random\Randomizer::shuffleBytes

(PHP 8 >= 8.2.0)

Random\Randomizer::shuffleBytes — Get a byte-wise permutation of a string

说明

public function Random\Randomizer::shuffleBytes(string $bytes): string

Returns a uniformly selected permutation of the input bytes.

Each possible permutation of the input bytes is equally likely to be returned.

参数

bytes

The string whose bytes are shuffled.

The input string will not be modified.

返回值

A permutation of the bytes of bytes.

错误/异常

示例

示例 #1 Random\Randomizer::shuffleBytes() example

<?php
$r = new \Random\Randomizer();

// Shuffle bytes in a string:
echo "«", $r->shuffleBytes("PHP is great!"), "»\n";
?>

以上示例的输出类似于:

« ga rHs!PPiet»

示例 #2 Byte-wise shuffling breaks Unicode characters

<?php
$r = new \Random\Randomizer();

$unicode = "🍎, 🥝, 🍌, 🍑, 🍇";
$shuffled = $r->shuffleBytes( $unicode );

// Byte-wise shuffling of non-ASCII characters destroys them,
// resulting in invalid sequences (indicated by the Unicode
// replacement character) or even entirely different characters
// appearing in the output.
echo "Original: ", $unicode, "\n";
echo "Shuffled: «", $shuffled, "»\n";
echo "Shuffled Bytes: ", bin2hex($shuffled), "\n";
?>

以上示例的输出类似于:

Original: 🍎, 🥝, 🍌, 🍑, 🍇
Shuffled: «� ��,�����🍟,� �� �, �,��»
Shuffled Bytes: 87208e912c8d9fa5f0f0f09f8d9f2cf09f208c9d20f02c209f2c8d8d

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。