RE: Regex

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Bruce,

Sure thing. So basically what I am trying to accomplish is when this
script runs, a menu is displayed to the user, it looks somewhat like
this:



Welcome
        To continue, please select your game mode.

        [Mode]  [Description]
        A.)     Mode A
        B.)     Mode B
        C.)     Mode C
        D.)     Mode D
        E.)     Mode E
        F.)     Mode F
        Q.)     Quit.

        To begin, please type the mode letter.

Mode:

 

Then, the below lines of code come into play:


	public function getSelection() {

		$choice =
$this->validateChoice(trim(strtoupper(fgets(STDIN))));
		return $choice;

	}

	private function validateChoice($choice) {

		$choice = preg_replace("/[\x00-\x1f]+/","",$choice);
		return $choice;

	}


So now the script is waiting for user input. The next thing to happen,
right after this code, is:


	private function validate($choice,$display,$input) {

		$valid = false;
		while(!$valid) {

			switch($choice) {

				case "Q":
					$display->writeCredits();
					sleep(4);
					exit(0);
				case "A":
					$valid = true;
					break;
				case "B":
					$valid = true;
					break;
				case "C":
					$valid = true;
					break;
				case "D":
					$valid = true;
					break;
				case "E":
					$valid = true;
					break;
				case "F":
					$valid = true;
					break;
				default:
					$display->writeInvalidChoice();
					$choice =
$input->getSelection();
					break;
		
			}

		}

		return $choice;

	}



Now, this is where the script loops infinitely. But, this is only the
first method which validates user input. There are several others.
Basically, my script should accept user input, strip anything that will
not be needed (like anything other than letters and numbers) and then
allow the user to proceed, where the script checks to see if the
numbers/letters they entered correspond to the menu's in any way.

Everything in the script runs perfect, except the fact that the control
statements print infinitely. And while I am sure there is something I
could do to this method to make it not loop infinitely, I would also
need to do this to several other loops. 


 
 
Thanks,
 
Jesse Hazen
-----Original Message-----
From: bruce [mailto:bedouglas@xxxxxxxxxxxxx] 
Sent: Thursday, March 26, 2009 5:23 PM
To: Hazen, Jesse, arvato digital services llc; php-general@xxxxxxxxxxxxx
Subject: RE:  Regex

hi...

if you haven't solved your issue... can you tell me in detail what
you're trying to accomplish? what are the steps to running the script?

thanks


-----Original Message-----
From: Jesse.Hazen@xxxxxxxxxxxxx [mailto:Jesse.Hazen@xxxxxxxxxxxxx]
Sent: Thursday, March 26, 2009 1:23 PM
To: php-general@xxxxxxxxxxxxx
Subject:  Regex


Hi,

 

Brand new to regex. So I have a cli which runs a regex on users input,
to make sure that only 0-9 and A-Z are accepted. It should strip
everything else. My problem is that when you press control-Z (on
Windows; I have not yet tested this on linux, and I will, but I would
like this to be compatible with both OS's) it loops infinitely saying
invalid data (because of the next method call, which decides what to do
based on your input). So, here is the input code. Is there a way I can
ensure that control commands are stripped, here?

 

 

 

 

 

            public function getSelection() {

 

                        $choice =
$this->validateChoice(trim(strtoupper(fgets(STDIN))));

                        return $choice;

 

            }

 

            private function validateChoice($choice) {

 

                        $choice =
ereg_replace("/[^0-9A-Z]/","",$choice);

                        return $choice;

 

            }

 

 

 

I have tried a ton of different things to try and fix this. Tried
/\c.|[^0-9A-Z]/, and many variations of \c and the [^0-9A-Z], to no
avail. I also tried using both preg_replace() as well as ereg_replace().
I spent a lot of time on the regex section of the PHP manual, but I am
not finding anything. Any advise on how to accomplish this?

 

 

 

Thanks,

 

Jesse Hazen


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux