PHP 8.2.4 Released!


(PHP 8 >= 8.2.0)

Random\Randomizer::shuffleBytesGet a byte-wise permutation of a string


public 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.

Liste de paramètres


The chaîne de caractères whose bytes are shuffled.

The input chaîne de caractères will not be modified.

Valeurs de retour

A permutation of the bytes of bytes.

Erreurs / Exceptions


Exemple #1 Random\Randomizer::shuffleBytes() example

= new \Random\Randomizer();

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

Résultat de l'exemple ci-dessus est similaire à :

« ga rHs!PPiet»

Exemple #2 Byte-wise shuffling breaks Unicode characters

= 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";
"Shuffled: «", $shuffled, "»\n";
"Shuffled Bytes: ", bin2hex($shuffled), "\n";

Résultat de l'exemple ci-dessus est similaire à :

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

User Contributed Notes

There are no user contributed notes for this page.
To Top