php generate random string
![]() |
| php generate random string |
Hi guys today we will learn how to generate random string in php..
just follow the below php rand string examples :
Example 1: php random string:
<?php
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@#$%^&*';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
// Echo the random string.
// Optionally, you can give it a desired string length.
echo generateRandomString();
?>
Example 2: php random string:
<?php
class Random{
public static function Numeric($length)
{
$chars = "1234567890";
$clen = strlen( $chars )-1;
$id = '';
for ($i = 0; $i < $length; $i++) {
$id .= $chars[mt_rand(0,$clen)];
}
return ($id);
}
public static function Alphabets($length)
{
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$clen = strlen( $chars )-1;
$id = '';
for ($i = 0; $i < $length; $i++) {
$id .= $chars[mt_rand(0,$clen)];
}
return ($id);
}
public static function AlphaNumeric($length)
{
$chars = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$clen = strlen( $chars )-1;
$id = '';
for ($i = 0; $i < $length; $i++) {
$id .= $chars[mt_rand(0,$clen)];
}
return ($id);
}
}
echo Random::Numeric(6).'<br/>'; #Example output: "567268"
echo Random::Alphabets(9).'<br/>'; #Example output : IAGRmZyJS
echo Random::AlphaNumeric(10); #Example output: Gzt6syUS8M
?>
That's it Guys How to Generate Random String in php..Thank You..


No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.