Home Backend Development PHP Tutorial PHP generates random string program code_PHP tutorial

PHP generates random string program code_PHP tutorial

Jul 13, 2016 pm 04:57 PM
php code Write function Can string tool us generate program this random need

Because the tool requires us to write a function that can generate random strings. I was thinking that there should be many such random string generating functions on the Internet. I searched a lot on Baidu. Now I will summarize these good PHP functions for you. .

Use a for loop to traverse the characters we defined

The code is as follows
 代码如下 复制代码


/* Generate Password
* Length : 8
*/
$str = “0123456789abcdefghijklmnopqrstuvwxyz”; // 输出字符集
$n = 8; // 输出串长度
$len = strlen($str)-1;
for($i=0 ; $i<$n; $i++){
$s .= $str[rand(0,$len)];
}
echo $s . “
”;
?>

Copy code



/* Generate Password
* Length: 8

*/
代码如下 复制代码

function getRandStr($length) {
$str = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randString = '';
$len = strlen($str)-1;
for($i = 0;$i < $length;$i ++){
$num = mt_rand(0, $len);
$randString .= $str[$num];
}
return $randString ;
}

//使用方法如下
$test=getRandStr($length=5);
echo $test;
?>

$str = “0123456789abcdefghijklmnopqrstuvwxyz”; // Output character set

$n = 8; // Output string length

$len = strlen($str)-1;
 代码如下 复制代码

/**
*/
function createRandomStr($length){
$str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';//62个字符
$strlen = 62;
while($length > $strlen){
   $str .= $str;
   $strlen += 62;
  }
  $str = str_shuffle($str);
  return substr($str,0,$length);
 }
echo createRandomStr(10);

?>

for($i=0; $i<$n; $i++){

$s .= $str[rand(0,$len)];

}
代码如下 复制代码

/**
*@blog
  */
 function createRandomStr($length){
  $str = array_merge(range(0,9),range('a','z'),range('A','Z'));
  shuffle($str);
  $str = implode('',array_slice($str,0,$length));
  return $str;
 }
 echo createRandomStr(10);
?> echo $s . “
”; ?> Can generate pure numeric strings, character strings, etc. of specified length. (Uppercase, lowercase, uppercase and lowercase, and a combination of uppercase and lowercase and numbers can also be expanded according to preference). $length=5 below, if you change it to 10, it will be 10 digits. Change $str = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' to $str = '0123456789' which is a pure numeric string.

The code is as follows Copy code
function getRandStr($length) { <🎜> $str = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; <🎜> $randString = ''; <🎜> $len = strlen($str)-1; <🎜> for($i = 0;$i < $length;$i ++){ <🎜> $num = mt_rand(0, $len); <🎜> $randString .= $str[$num]; <🎜> } <🎜> return $randString ; <🎜> }<🎜> <🎜>//How to use it is as follows<🎜> $test=getRandStr($length=5);<🎜> echo $test;<🎜> ?> or use while
The code is as follows Copy code
/**<🎜>  */<🎜> function createRandomStr($length){<🎜> $str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';//62 characters<🎜> $strlen = 62;<🎜> while($length > $strlen){ $str .= $str; $strlen += 62; } $str = str_shuffle($str); Return substr($str,0,$length); } echo createRandomStr(10); ?> Use the idea of ​​array and character conversion:
The code is as follows Copy code
/**<🎜>   *@blog  *@blog */ function createRandomStr($length){ $str = array_merge(range(0,9),range('a','z'),range('A','Z')); shuffle($str); $str = implode('',array_slice($str,0,$length)); return $str; } echo createRandomStr(10); ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631543.htmlTechArticleBecause the tool requires us to write a function that can generate random strings. I think there should be many such functions on the Internet. Generate random string functions, Baidu has a lot of them, and I will give them below...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
How to register in the ok exchange in China? ok trading platform registration and use guide for beginners in mainland China How to register in the ok exchange in China? ok trading platform registration and use guide for beginners in mainland China May 08, 2025 pm 10:51 PM

In the cryptocurrency market, choosing a reliable trading platform is crucial. As a world-renowned digital asset exchange, the OK trading platform has attracted a large number of novice users in mainland China. This guide will introduce in detail how to register and use it on the OK trading platform to help novice users get started quickly.

TOP10 futures trading platforms: Perpetual contracts and options trading TOP10 futures trading platforms: Perpetual contracts and options trading May 08, 2025 pm 07:12 PM

In the cryptocurrency market, futures trading platforms play an important role, especially in perpetual contracts and options trading. Here are the top ten highly respected futures trading platforms in the market, and provide detailed introduction to their characteristics and advantages in perpetual contract and option trading.

Top 10 cryptocurrency platforms in the world that support multi-chain transactions are authoritatively released in 2025 Top 10 cryptocurrency platforms in the world that support multi-chain transactions are authoritatively released in 2025 May 08, 2025 pm 07:15 PM

According to the latest evaluations and industry trends from authoritative institutions in 2025, the following are the top ten cryptocurrency platforms in the world that support multi-chain transactions, combining transaction volume, technological innovation, compliance and user reputation comprehensive analysis:

PHP Email: Step-by-Step Sending Guide PHP Email: Step-by-Step Sending Guide May 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

Go: String Manipulation with the Standard 'strings' Package Go: String Manipulation with the Standard 'strings' Package May 09, 2025 am 12:07 AM

Go uses the "strings" package for string operations. 1) Use strings.Join function to splice strings. 2) Use the strings.Contains function to find substrings. 3) Use the strings.Replace function to replace strings. These functions are efficient and easy to use and are suitable for various string processing tasks.

Top 10 cryptocurrency exchanges in the currency circle, the latest ranking of the top 10 digital currency trading platforms in 2025 Top 10 cryptocurrency exchanges in the currency circle, the latest ranking of the top 10 digital currency trading platforms in 2025 May 08, 2025 pm 10:45 PM

Ranking of the top ten cryptocurrency exchanges in the currency circle: 1. Binance: Leading the world, providing efficient trading and a variety of financial products. 2. OKX: It is innovative and diverse, supporting a variety of transaction types. 3. Huobi: Stable and reliable, with high-quality service. 4. Coinbase: Be friendly for beginners and simple interface. 5. Kraken: The first choice for professional traders, with powerful tools. 6. Bitfinex: efficient trading, rich trading pairs. 7. Bittrex: Safety compliance, regulatory cooperation. 8. Poloniex and so on.

Does Binance have a mobile app? Binance binance official website mobile version Android APP recommendation Does Binance have a mobile app? Binance binance official website mobile version Android APP recommendation May 08, 2025 pm 10:12 PM

Binance, as the world's leading cryptocurrency trading platform, provides a variety of ways for users to trade and manage assets easily. Among them, Binance Mobile APP is one of the tools chosen by many users. The following details the download and use of Binance's official Android APP.

Guide to Sending Emails with PHP & SMTP Guide to Sending Emails with PHP & SMTP May 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

See all articles