Jump to content

How do I do this simple array thing...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Anicho

Anicho

    Newbie

  • Members
  • PipPip
  • 14 posts
I'm making a simple PHP Random Script Generator
and just got future's reference, how would I add spaces, and symbols in a array?

array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0");

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
random script generator? or do you mean password generator?

space is added as " ", symbols are added as "@", "&", "{" etc...

otherwise you could do something like this:


	function generatepw($chars = 8) {

		$pw = "";

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

			switch (rand(0, 2)) {

				case 0:

					$pw .= chr(rand(ord('a'), ord('z')));

					break;

				case 1:

					$pw .= chr(rand(ord('A'), ord('Z')));

					break;

				case 2:

					$pw .= chr(rand(ord('0'), ord('9')));

					break;

			}

		}

		return $pw;

	}


This function randomizes uppercases, lowercases and digits. for symbols: you can add a fourth thing like this:


case 3:

     $symb = Array("!", "#", "%", "&", "=", "?", "*", "+", "-", "_", "ยง");

     $pw .= $symb[rand(0,count($symb)-1)];

     break;

just don't forget to add the first rand statement (in the switch) from 0, 2 to 0, 3...
and add the symbols you want to use to the array. the randomizer checks how many symbols you've added itself. so just add and remove symbols and it's done.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall