[PHP] PHP-CLI issue
Hi, I am working on building a PHP-CLI 'game' for converting binary/hex/decimal, to better your speed at converting between base number systems. I started on the game class, and created a little menu with user input for different game modes. If the user inputs invalid data, the script is supposed to say 'invalid' and ask for the input again. Then, as long as the data remains invalid, the user is continuously prompted until the data is valid, whereas the correct method is called. My issue is that when invalid data is input, the 'invalid, try again' message is displayed 3 times before the script stops for the user to try again. So instead of: Invalid data. Mode: (here, waits for the user to input) it says: Invalid data. Mode: Invalid data. Mode: Invalid data. Mode: (here, waits for the user to input) I have tried altering my loop a few different ways, to no avail. This is also my very first php-cli and so I am a bit of a newb when it comes to php-cli. The code is below. I'm not worried about other portions of the code at this point; can anyone see what I am doing incorrectly that is looping this message three times instead of just displaying once? class BaseGame { public function run() { fwrite(STDOUT,$this->welcome()); $input = $this->getSelection(); $this->decideMode($input); } private function welcome() { $menu = "Welcome to the Binary/Hexadecimal conversion Practice Tool.\n"; $menu.= "This tool is used as a game for learning binary/hexadecimal/decimal conversion.\n\n"; $menu.= "\tTo continue, please select your game mode.\n\n"; $menu.= "\t[Mode]\t[Description]\n"; $menu.= "\tA.)\tGiven a random binary value, resolve to decimal.\n"; $menu.= "\tB.)\tGiven a random binary value, resolve to hexadecimal.\n"; $menu.= "\tC.)\tGiven a random hexadecimal value, resolve to binary.\n"; $menu.= "\tD.)\tGiven a random hexadecimal value, resolve to decimal.\n"; $menu.= "\tE.)\tGiven a random decimal value, resolve to binary.\n"; $menu.= "\tF.)\tGiven a random decimal value, resolve to hexadecimal.\n"; $menu.= "\tG.)\tGiven two random values, decide which is larger.\n"; $menu.= "\tQ.)\tQuit.\n\n"; $menu.= "\tTo begin, simply type the mode letter.\n\n"; return $menu; } private function getSelection() { fwrite(STDOUT,"Mode: "); $input = strtoupper(fgetc(STDIN)); return $input; } private function decideMode($input) { while($input) { if($input == "Q") { exit(0); } elseif($input == "A") { $this->binary2decimal(); } elseif($input == "B") { $this->binary2hexadecimal(); } elseif($input == "C") { $this->hexadecimal2binary(); } elseif($input == "D") { $this->hexadecimal2decimal(); } elseif($input == "E") { $this->decimal2binary(); } elseif($input == "F") { $this->decimal2hexadecimal(); } elseif($input == "G") { $this->comparison(); } else { fwrite(STDOUT,"\nInvalid choice. Choose again.\n"); $input = $this->getSelection(); } } exit(0); }
RE: [PHP] PHP-CLI issue
Rob, Thanks for the quick reply. I did try that before, but now my issue is that the if-else section does not recognize the data as being valid. So, I changed it to fgets(), and reran, this time using valid data. The initial problem is corrected (it only gives the error once), but now it does not recognize my data, and I get the error once no matter what I input. Is there anything in particular I would need to do to the variable, after the STDIN, to prepare it for the validation? Thanks, Jesse Hazen -Original Message- From: Robert Cummings [mailto:rob...@interjinn.com] Sent: Wednesday, March 18, 2009 7:00 AM To: Hazen, Jesse, arvato digital services llc Cc: php-general@lists.php.net Subject: Re: [PHP] PHP-CLI issue On Wed, 2009-03-18 at 06:52 -0700, jesse.ha...@arvatousa.com wrote: > private function getSelection() { > > fwrite(STDOUT,"Mode: "); > > $input = strtoupper(fgetc(STDIN)); > > return $input; > > } Use fgets() instead of fgetc(). You're retrieving one character at a time. When someone hits enter... 1 (or in winblows 2) extra characters are usually put on the input stack that represent the enter key itself. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] PHP-CLI issue
Rob, Works like a charm! Much appreciated. Thanks, Jesse Hazen -Original Message- From: Robert Cummings [mailto:rob...@interjinn.com] Sent: Wednesday, March 18, 2009 7:24 AM To: Hazen, Jesse, arvato digital services llc Cc: php-general@lists.php.net Subject: RE: [PHP] PHP-CLI issue On Wed, 2009-03-18 at 07:10 -0700, jesse.ha...@arvatousa.com wrote: > Rob, > > Thanks for the quick reply. I did try that before, but now my issue is > that the if-else section does not recognize the data as being valid. So, > I changed it to fgets(), and reran, this time using valid data. The > initial problem is corrected (it only gives the error once), but now it > does not recognize my data, and I get the error once no matter what I > input. Is there anything in particular I would need to do to the > variable, after the STDIN, to prepare it for the validation? Use trim() to trim the retrieved input since it will have the newline appended to it. Then you can do your comparison. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Frameworks / obstinate?
Not to mention the Object Oriented nature of PHP. This looks like a pretty cool idea, but JS OO cannot compare to PHP OO programming. Thanks, Jesse Hazen -Original Message- From: Stuart [mailto:stut...@gmail.com] Sent: Monday, March 23, 2009 7:49 AM To: tedd Cc: php-general@lists.php.net Subject: Re: [PHP] Frameworks / obstinate? 2009/3/23 tedd : > However, I have heard of "new" javascript being run server-side. What's the > likelihood of that "catching on" and surpassing php? http://aptana.com/jaxer I really like the idea, but I'm yet to have a good reason to try it. If you're starting from scratch it has the advantage of limiting the skills required. Jaxar sits on top of Apache so I'm not sure what the performance is like. Either way I don't see it gaining much traction these days, at least not quickly. -Stuart -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] 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
RE: [PHP] Regex
Nitsan, Thank you very much for the input. However, I just gave this a try and it still did not strip the control-Z. Therefore, it is still hitting my loop later and looping infinitely. I am sure I could mend that by working something into that loop, but there are several loops I would need to do this on. If I can stop the command at this method, then the entire script will run perfectly. Thanks, Jesse Hazen _ arvato digital services llc A Bertelsmann Company 29011 Commerce Center Dr. Valencia, CA 91355 http://arvatodigitalservices.com http://arvatodigitalservices.com> jesse.ha...@arvatousa.com mailto:jesse.ha...@arvatousa.com> Tel. +1 (661) 702-2727 Fax. +1 (661) 775-6478 From: nit...@binnun.co.il [mailto:nit...@binnun.co.il] On Behalf Of Nitsan Bin-Nun Sent: Thursday, March 26, 2009 1:44 PM To: Hazen, Jesse, arvato digital services llc Cc: php-general@lists.php.net Subject: Re: [PHP] Regex To filter out everything which is not A-Z, a-z and 0-9 try this regex: $str = preg_replace("#[^a-zA-Z0-9]+#is", "", $str); On Thu, Mar 26, 2009 at 10:23 PM, wrote: 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
RE: [PHP] Regex
Nitsan, Thanks again. Sad to say, same result. The second option looped an error: Warning: preg_replace(): Compilation failed: nothing to repeat at offset 17 Thanks, Jesse Hazen From: nit...@binnun.co.il [mailto:nit...@binnun.co.il] On Behalf Of Nitsan Bin-Nun Sent: Thursday, March 26, 2009 1:58 PM To: Hazen, Jesse, arvato digital services llc Cc: php-general@lists.php.net Subject: Re: [PHP] Regex I have no idea about this control-Z thing, you might want to try it with UTF (just add the 'u' modificator): $str = preg_replace("#[^a-zA-Z0-9]+#uis", "", $str); Or try this: $str = preg_replace("#(\b[^a-zA-Z0-9]\b?)+#uis", "", $str); I may be wrong about the second one but it might work... On Thu, Mar 26, 2009 at 10:51 PM, wrote: Nitsan, Thank you very much for the input. However, I just gave this a try and it still did not strip the control-Z. Therefore, it is still hitting my loop later and looping infinitely. I am sure I could mend that by working something into that loop, but there are several loops I would need to do this on. If I can stop the command at this method, then the entire script will run perfectly. Thanks, Jesse Hazen _ arvato digital services llc A Bertelsmann Company 29011 Commerce Center Dr. Valencia, CA 91355 http://arvatodigitalservices.com jesse.ha...@arvatousa.com Tel. +1 (661) 702-2727 Fax. +1 (661) 775-6478 From: nit...@binnun.co.il [mailto:nit...@binnun.co.il] On Behalf Of Nitsan Bin-Nun Sent: Thursday, March 26, 2009 1:44 PM To: Hazen, Jesse, arvato digital services llc Cc: php-general@lists.php.net Subject: Re: [PHP] Regex To filter out everything which is not A-Z, a-z and 0-9 try this regex: $str = preg_replace("#[^a-zA-Z0-9]+#is", "", $str); On Thu, Mar 26, 2009 at 10:23 PM, wrote: 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
RE: [PHP] Regex
Nitsan, Not a problem, thanks for the help. So, it is printed as ^Z. However, I created a little test.php script to accept input and print it back to me. So, I used control z as my test, and it simply printed a line, and that's all. So, then I input control z, and then had it print a letter right after ("a"), and it was just the letter a. no whitespace, no breaks, nothing. I even tried one more thing: in the loop (not in the code below) I put unset($choice), right above where it tells the user their input is invalid and asks for it again. Even this did not change anything: when pressing control - z, it looped infinitely. I assumed that \c would be the regex to mend this, as the php manual has this as "control-x, where x is any character". I even tried every variation of this, with a key combination like \cZ, or \cz, or \c[zZ], yet it still does not catch it. Thanks, Jesse Hazen From: nit...@binnun.co.il [mailto:nit...@binnun.co.il] On Behalf Of Nitsan Bin-Nun Sent: Thursday, March 26, 2009 2:09 PM To: Hazen, Jesse, arvato digital services llc Cc: php-general@lists.php.net Subject: Re: [PHP] Regex If you can point me on the character which control-z creates it would make it easier, I have no idea of it ;) I'm sorry mate. On Thu, Mar 26, 2009 at 11:06 PM, wrote: Nitsan, Thanks again. Sad to say, same result. The second option looped an error: Warning: preg_replace(): Compilation failed: nothing to repeat at offset 17 Thanks, Jesse Hazen From: nit...@binnun.co.il [mailto:nit...@binnun.co.il] On Behalf Of Nitsan Bin-Nun Sent: Thursday, March 26, 2009 1:58 PM To: Hazen, Jesse, arvato digital services llc Cc: php-general@lists.php.net Subject: Re: [PHP] Regex I have no idea about this control-Z thing, you might want to try it with UTF (just add the 'u' modificator): $str = preg_replace("#[^a-zA-Z0-9]+#uis", "", $str); Or try this: $str = preg_replace("#(\b[^a-zA-Z0-9]\b?)+#uis", "", $str); I may be wrong about the second one but it might work... On Thu, Mar 26, 2009 at 10:51 PM, wrote: Nitsan, Thank you very much for the input. However, I just gave this a try and it still did not strip the control-Z. Therefore, it is still hitting my loop later and looping infinitely. I am sure I could mend that by working something into that loop, but there are several loops I would need to do this on. If I can stop the command at this method, then the entire script will run perfectly. Thanks, Jesse Hazen _ arvato digital services llc A Bertelsmann Company 29011 Commerce Center Dr. Valencia, CA 91355 http://arvatodigitalservices.com jesse.ha...@arvatousa.com Tel. +1 (661) 702-2727 Fax. +1 (661) 775-6478 From: nit...@binnun.co.il [mailto:nit...@binnun.co.il] On Behalf Of Nitsan Bin-Nun Sent: Thursday, March 26, 2009 1:44 PM To: Hazen, Jesse, arvato digital services llc Cc: php-general@lists.php.net Subject: Re: [PHP] Regex To filter out everything which is not A-Z, a-z and 0-9 try this regex: $str = preg_replace("#[^a-zA-Z0-9]+#is", "", $str); On Thu, Mar 26, 2009 at 10:23 PM, wrote: 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
RE: [PHP] Regex
Nistan, Just got home, tested on linux. No problem on linux, the control-z just exits. I may just go ahead and post my issue to the PHP windows list to see if anything comes up, and not worry if it doesnt. I appreciate the help very much Thanks, Jesse -Original Message- From: jesse.ha...@arvatousa.com [mailto:jesse.ha...@arvatousa.com] Sent: Thu 3/26/2009 2:17 PM To: nitsa...@gmail.com Cc: php-general@lists.php.net Subject: RE: [PHP] Regex Nitsan, Not a problem, thanks for the help. So, it is printed as ^Z. However, I created a little test.php script to accept input and print it back to me. So, I used control z as my test, and it simply printed a line, and that's all. So, then I input control z, and then had it print a letter right after ("a"), and it was just the letter a. no whitespace, no breaks, nothing. I even tried one more thing: in the loop (not in the code below) I put unset($choice), right above where it tells the user their input is invalid and asks for it again. Even this did not change anything: when pressing control - z, it looped infinitely. I assumed that \c would be the regex to mend this, as the php manual has this as "control-x, where x is any character". I even tried every variation of this, with a key combination like \cZ, or \cz, or \c[zZ], yet it still does not catch it. Thanks, Jesse Hazen From: nit...@binnun.co.il [mailto:nit...@binnun.co.il] On Behalf Of Nitsan Bin-Nun Sent: Thursday, March 26, 2009 2:09 PM To: Hazen, Jesse, arvato digital services llc Cc: php-general@lists.php.net Subject: Re: [PHP] Regex If you can point me on the character which control-z creates it would make it easier, I have no idea of it ;) I'm sorry mate. On Thu, Mar 26, 2009 at 11:06 PM, wrote: Nitsan, Thanks again. Sad to say, same result. The second option looped an error: Warning: preg_replace(): Compilation failed: nothing to repeat at offset 17 Thanks, Jesse Hazen From: nit...@binnun.co.il [mailto:nit...@binnun.co.il] On Behalf Of Nitsan Bin-Nun Sent: Thursday, March 26, 2009 1:58 PM To: Hazen, Jesse, arvato digital services llc Cc: php-general@lists.php.net Subject: Re: [PHP] Regex I have no idea about this control-Z thing, you might want to try it with UTF (just add the 'u' modificator): $str = preg_replace("#[^a-zA-Z0-9]+#uis", "", $str); Or try this: $str = preg_replace("#(\b[^a-zA-Z0-9]\b?)+#uis", "", $str); I may be wrong about the second one but it might work... On Thu, Mar 26, 2009 at 10:51 PM, wrote: Nitsan, Thank you very much for the input. However, I just gave this a try and it still did not strip the control-Z. Therefore, it is still hitting my loop later and looping infinitely. I am sure I could mend that by working something into that loop, but there are several loops I would need to do this on. If I can stop the command at this method, then the entire script will run perfectly. Thanks, Jesse Hazen _ arvato digital services llc A Bertelsmann Company 29011 Commerce Center Dr. Valencia, CA 91355 http://arvatodigitalservices.com jesse.ha...@arvatousa.com Tel. +1 (661) 702-2727 Fax. +1 (661) 775-6478 From: nit...@binnun.co.il [mailto:nit...@binnun.co.il] On Behalf Of Nitsan Bin-Nun Sent: Thursday, March 26, 2009 1:44 PM To: Hazen, Jesse, arvato digital services llc Cc: php-general@lists.php.net Subject: Re: [PHP] Regex To filter out everything which is not A-Z, a-z and 0-9 try this regex: $str = preg_replace("#[^a-zA-Z0-9]+#is", "", $str); On Thu, Mar 26, 2009 at 10:23 PM, wrote: 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?
RE: [PHP] Regex
Jochem, Thanks, but this regex did not seem to work for me. At least, not on Windows. I will not be able to test on linux for a few hours. I spoke too soon yesterday, too. Control-Z is a problem on Windows, and not on Linux. However, on linux, control-D is a problem. Thanks, Jesse Hazen -Original Message- From: Jochem Maas [mailto:joc...@iamjochem.com] Sent: Thursday, March 26, 2009 11:45 PM To: Hazen, Jesse, arvato digital services llc Cc: php-general@lists.php.net Subject: Re: [PHP] Regex jesse.ha...@arvatousa.com schreef: > 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? > there is, your control-Z is not a Z at all, and it's only printed as ^Z so you can see it ... it's actually a non-printing char. try this regexp for stripping control chars: /[\x00-\x1f]+/ > > > > > > > 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
RE: [PHP] Regex
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:bedoug...@earthlink.net] Sent: Thursday, March 26, 2009 5:23 PM To: Hazen, Jesse, arvato digital services llc; php-general@lists.php.net Subject: RE: [PHP] 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.ha...@arvatousa.com [mailto:jesse.ha...@arvatousa.com] Sent: Thursday, March 26, 2009 1:23 PM To: php-general@lists.php.net Subject: [PHP] 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 manua
RE: [PHP] Regex
Jochem, To be more specific, the error I get when using this regex is No ending delimiter '/' found Thanks, Jesse Hazen -Original Message- From: Jochem Maas [mailto:joc...@iamjochem.com] Sent: Thursday, March 26, 2009 11:45 PM To: Hazen, Jesse, arvato digital services llc Cc: php-general@lists.php.net Subject: Re: [PHP] Regex jesse.ha...@arvatousa.com schreef: > 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? > there is, your control-Z is not a Z at all, and it's only printed as ^Z so you can see it ... it's actually a non-printing char. try this regexp for stripping control chars: /[\x00-\x1f]+/ > > > > > > > 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