Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

โปรแกรม PHP วิธีต่างๆ ในการสร้างสตริงสุ่ม


การใช้ฟังก์ชัน bin2hex

ตัวอย่าง

<?php
$num = 12;
$res = bin2hex(random_bytes($num));
print_r("The randomly generated string is : ");
echo $res;
?>

ผลลัพธ์

The randomly generated string is : f1db16115fa93b98493d388b

มีการกำหนดตัวเลขและเรียกใช้ฟังก์ชัน bin2hex บนหมายเลขนี้ ภายในฟังก์ชัน bin2hex ฟังก์ชัน 'random_bytes' จะถูกเรียกใช้บนหมายเลขนี้ สตริงสุ่มที่สร้างขึ้นจะถูกพิมพ์บนคอนโซล

การใช้ฟังก์ชันแฮช

ตัวอย่าง

<?php
$my_str = rand();
$res = sha1($my_str);
print_r("The randomly generated string using hashing function sha1 is :");
echo $res;
?>

ผลลัพธ์

The randomly generated string using hashing function sha1 is :9a4a73c35ac034832332977f3d5accd8eace5260

ตัวเลขถูกกำหนดโดยการเรียกฟังก์ชัน 'rand' ฟังก์ชันแฮช sha1 ถูกเรียกใช้บนหมายเลขที่สร้างแบบสุ่มนี้ สตริงสุ่มที่สร้างขึ้นจะถูกพิมพ์บนคอนโซล

การใช้ฟังก์ชัน uniqid ในตัว

ตัวอย่าง

<?php
$res = uniqid();
print_r("The randomly generated string using uniqid function is : ");
echo $res;
?>

ผลลัพธ์

The randomly generated string using uniqid function is : 5ed4b884cef34

ตัวเลขถูกกำหนดโดยการเรียกฟังก์ชัน 'rand' ฟังก์ชันแฮช sha1 ถูกเรียกใช้บนหมายเลขที่สร้างแบบสุ่มนี้ สตริงสุ่มที่สร้างขึ้นจะถูกพิมพ์บนคอนโซล