RE: [PHP] Regex
| From: | Jesse dot Hazen at arvatousa dot com | Date: | Fri, 27 Mar 2009 13:54:32 +0000 |
| Subject: | RE: [PHP] Regex | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-290749@lists.php.net to get a copy of this message | ||
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:jochem@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.Hazen@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
>
>