Home  >  Article  >  Backend Development  >  Generate random string

Generate random string

巴扎黑
巴扎黑Original
2016-12-01 10:03:561516browse

##########javascript 版############### 

function randomChar(l) { 
  var x="123456789poiuytrewqasdfghjklmnbvcxzQWERTYUIPLKJHGFDSAZXCVBNM"; 
  var tmp=""; 
  for(var i=0;i< l;i++) { 
  tmp += x.charAt(Math.ceil(Math.random()*100000000)%x.length); 
  } 
  return tmp; 

var str = randomChar(20); 
document.write(str.toUpperCase()); 



##########php版############### 

function randomChar($len){ 
if($len<1) return; 
$str=''; 
$ss = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; 
for($i=0;$i<$len;$i++){ 
  $n = rand(0,35); 
  $str .= $ss[$n]; 

return $str; 

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