shuffle a string in php

To shuffle a string in PHP, we can convert the string to an array, use the shuffle() function to shuffle the array, and then convert the array back to a string using the implode() function:

main.php
$string = "Hello world!";
$characters = str_split($string); // convert string to array of characters
shuffle($characters); // shuffle the array
$shuffledString = implode('', $characters); // convert array back to string
echo $shuffledString;
242 chars
6 lines

Output: oHrdell wlo!

related categories

gistlibby LogSnag