Home >Backend Development >PHP Tutorial >PHP method to obtain a specified number of random strings

PHP method to obtain a specified number of random strings

墨辰丷
墨辰丷Original
2018-05-26 10:43:081920browse

This article mainly introduces the method of obtaining a specified number of random strings in PHP, involving PHP's operation skills related to array traversal and string operations. Friends in need can refer to it

The details are as follows:

function getARandLetter($number = 1) {
 if ($number == 0)
  return FALSE; //去除0
 $number = $number < 0 ? - $number : $number; //如果小于零取正值
 $letterArr = array (&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;, &#39;I&#39;, &#39;J&#39;, &#39;K&#39;, &#39;L&#39;, &#39;M&#39;, &#39;N&#39;, &#39;O&#39;, &#39;P&#39;, &#39;Q&#39;, &#39;R&#39;, &#39;S&#39;, &#39;T&#39;, &#39;U&#39;, &#39;V&#39;, &#39;W&#39;, &#39;X&#39;, &#39;Y&#39;, &#39;Z&#39;, &#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;, &#39;h&#39;, &#39;i&#39;, &#39;j&#39;, &#39;k&#39;, &#39;l&#39;, &#39;m&#39;, &#39;n&#39;, &#39;o&#39;, &#39;p&#39;, &#39;q&#39;, &#39;r&#39;, &#39;s&#39;, &#39;t&#39;, &#39;u&#39;, &#39;v&#39;, &#39;w&#39;, &#39;x&#39;, &#39;y&#39;, &#39;z&#39; );
 $returnStr =&#39;&#39;;
 for($i= 0; $i < $number; $i ++) {
  $returnStr .= $letterArr [rand ( 0, 51 )];
 }
 return $returnStr;
}
echo getARandLetter(8);

Running results: lUJfScvS

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHP implementationRandomlyGet domain names that are not blocked by WeChat

PHP method to implement randomgenerate watermark image function

php Get random## from the specified number #Combined method

The above is the detailed content of PHP method to obtain a specified number of random strings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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