Home Backend Development PHP Tutorial PHP String Exercise 1: Generate Random Password from Given String

PHP String Exercise 1: Generate Random Password from Given String

Jul 30, 2021 am 10:35 AM
php string

Welcome everyone to my channel! Today I started playing a new series, which is about how to play PHP strings. Don’t miss it if you pass by. Everyone is also welcome to give reasonable suggestions. I am not a big boss, I am just dedicated to getting new people started, but the technology is big For my senior explanation, you can watch "PHP Video Tutorial", everything you want is here!

So, let’s start the text!

As the title states, the center of this article is about the implementation of generating random passwords. So when implementing random numbers in PHP, everyone’s first reaction is probably the rand function, but today we are not allowed to use rand! (For the use of rand, you can read "PHP Mathematical Function Practice 3: Clever Use of Random Function rand()")

Without rand(), there are naturally other methods. I will give it below. Let's introduce another simple method to implement random numbers:

Directly code:

<?php
function password_generate($chars)
{
$data = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz';
  return substr(str_shuffle($data), 0, $chars);
}
  echo password_generate(7)."\n";
Copy after login

First define a password_generate method, then define a string in the method body, and then combine str_shuffle and substrh function to generate.

The string we are given here is "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz", and the requirement is to have a random password in this string.

The running results are as follows:

GIF 2021-7-30 星期五 上午 10-12-57.gif

It’s still very simple!

Here, we use two important functions, the str_shuffle() and substr() functions.

str_shuffle() The function is used to randomly shuffle all characters in a string, and its syntax is "str_shuffle(string)". The return value is a scrambled string.

substr() The function is used to return a part of the string. Its syntax is "substr(string,start,length)" and the return value is the return string. The extraction part, if it fails, returns FALSE, or returns an empty string.

It should be noted that if the start parameter is a negative number and length is less than or equal to start, then length is 0.

Finally, I would like to recommend a classic course on our platform "PHP String Processing (Jade Girl Heart Sutra Edition)". It's free~ Come and learn!

The above is the detailed content of PHP String Exercise 1: Generate Random Password from Given String. For more information, please follow other related articles on the PHP Chinese website!

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 Article

Hot Article

Hot Article Tags

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)

How to convert characters to ascii code in php How to convert characters to ascii code in php Mar 03, 2023 pm 06:55 PM

How to convert characters to ascii code in php

How to replace a certain character to be empty in a php string How to replace a certain character to be empty in a php string Mar 06, 2023 pm 06:39 PM

How to replace a certain character to be empty in a php string

How to remove all uppercase letters from string in php How to remove all uppercase letters from string in php Sep 26, 2022 pm 07:59 PM

How to remove all uppercase letters from string in php

How to extract only Chinese characters from php string How to extract only Chinese characters from php string Sep 22, 2022 pm 07:44 PM

How to extract only Chinese characters from php string

How to remove double quotes from string in php How to remove double quotes from string in php Mar 28, 2023 pm 04:54 PM

How to remove double quotes from string in php

What are the string delimiters in php? What are the string delimiters in php? Sep 23, 2022 pm 05:48 PM

What are the string delimiters in php?

How to convert data into hexadecimal string in php How to convert data into hexadecimal string in php Aug 25, 2022 pm 06:28 PM

How to convert data into hexadecimal string in php

How to remove left and right characters from string in php How to remove left and right characters from string in php Mar 27, 2023 pm 03:29 PM

How to remove left and right characters from string in php

See all articles