Ereg problems
am 16.01.2007 04:59:46 von Beauford
Hi,
Without getting into a long drawn out discussion, can anyone show me a way
to validate a string that is input from a form.
I want to allow the following: a to z, A to Z, 0 to 9, and "_-'" (without
the double quotes) and spaces. I have now been screwing around with this for
over 8 hours. There are other issues as well that has been compoudning this,
but this would be a great start to solving my issue.
Thanks
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Ereg problems
am 16.01.2007 12:38:56 von Niel Archer
Hi
> I want to allow the following: a to z, A to Z, 0 to 9, and "_-'" (without
> the double quotes) and spaces. I have now been screwing around with this for
> over 8 hours. There are other issues as well that has been compoudning this,
> but this would be a great start to solving my issue.
I don't use ereg (prefer preg_match) but unless the regex syntax is
greatly different try: "$[\w'-]+$"
Niel
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Ereg problems
am 16.01.2007 12:57:58 von Niel Archer
Oops that should be "^[\w'-]+$"
Niel
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Ereg problems
am 16.01.2007 17:20:39 von Beauford
Nope, doesn't work for me. I think what I'm trying to do must not be
possible. I've been at this for two days now. I changed yours to this - ereg
("([0-9][A-Z][a-z][-_.'\ ])" but it always says that it is invalid. The
last one after the slash is supposed to be a space - I have to have a space.
I don't need a length.
> -----Original Message-----
> From: bedul [mailto:landavia81@gmail.com]
> Sent: January 15, 2007 11:48 PM
> To: Beauford
> Subject: Re: [PHP-WIN] Ereg problems
>
>
> $nlong=strlen($username);
> if(ereg ("([0-9][A-Z][a-z][_-]{$nlong})", $username){
> echo "allowed";
> }else{
> echo "disallowed";
>
> }
>
> //taken from ereg help
> /*
> if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
> echo "$regs[3].$regs[2].$regs[1]";
> } else {
> echo "Invalid date format: $date";
> }
> */
> ?>
>
> ----- Original Message -----
> From: "Beauford"
> To:
> Sent: Tuesday, January 16, 2007 10:59 AM
> Subject: [PHP-WIN] Ereg problems
>
>
> > Hi,
> >
> > Without getting into a long drawn out discussion, can
> anyone show me a
> > way to validate a string that is input from a form.
> >
> > I want to allow the following: a to z, A to Z, 0 to 9, and "_-'"
> > (without the double quotes) and spaces. I have now been screwing
> > around with this
> for
> > over 8 hours. There are other issues as well that has been
> compoudning
> this,
> > but this would be a great start to solving my issue.
> >
> > Thanks
> >
> > --
> > PHP Windows Mailing List (http://www.php.net/) To
> unsubscribe, visit:
> > http://www.php.net/unsub.php
> >
>
>
>
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Ereg problems
am 16.01.2007 17:46:21 von Niel Archer
Hi
What you've written doesn't match the description you gave. As a
regular expression "([0-9][A-Z][a-z][-_.'\ ])" means a digit followed by
an uppercase letter followed by a lower case letter followed by (hyphen
or underscore or mid-dot or single quote or space). This is a total of
four characters only.
Is your string only supposed to be four characters in that format?
I had assumed from your original post, that you wanted a variable
length string, that could only contain alphanumeric characters plus
hyphen, underscore, and single quote (I missed the space). All of which
in any position. If this is not the case please describe exactly what
it is you're trying to achieve.
Niel
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Ereg problems
am 16.01.2007 17:59:09 von Daniel Anderson
------=_NextPart_000_004C_01C7398F.A3E68A90
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
I think this is your solution:
------------------------------------------------------------ -------------=
-------
$string =3D "~YOUR STRING~"
if (!eregi_replace("[0-9A-Za-z -_']","",$string))
{
echo "string allowed";
}
else {
echo "string disallowed";
}
------------------------------------------------------------ -------------=
-------
I tested it with the following strings:
$string =3D "Daniel Anderson"
Returned "string allowed".
$string =3D "Daniel Anderson!!"
Returned "string disallowed".
$string =3D "Daniel, yo Anderson"
Returned "string disallowed".
$string =3D "Daniel_Anderson-Innit"
Returned "string allowed"
I think that may be your solution :)
Hope it helps! Try to keep all the collections like "a-z" within one set =
of square brackets.
Warmest Regards,
Dan
----- Original Message -----=20
From: "Beauford"
To:
Sent: Tuesday, January 16, 2007 4:20 PM
Subject: RE: [PHP-WIN] Ereg problems
> Nope, doesn't work for me. I think what I'm trying to do must not be
> possible. I've been at this for two days now. I changed yours to this =
- ereg
> ("([0-9][A-Z][a-z][-_.'\ ])" but it always says that it is invalid. =
The
> last one after the slash is supposed to be a space - I have to have a =
space.
> I don't need a length.
>=20
>> -----Original Message-----
>> From: bedul [mailto:landavia81@gmail.com]=20
>> Sent: January 15, 2007 11:48 PM
>> To: Beauford
>> Subject: Re: [PHP-WIN] Ereg problems
>>=20
>>
>> $nlong=3Dstrlen($username);
>> if(ereg ("([0-9][A-Z][a-z][_-]{$nlong})", $username){
>> echo "allowed";
>> }else{
>> echo "disallowed";
>>=20
>> }
>>=20
>> //taken from ereg help
>> /*
>> if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
>> echo "$regs[3].$regs[2].$regs[1]";
>> } else {
>> echo "Invalid date format: $date";
>> }
>> */
>> ?>
>>=20
>> ----- Original Message -----
>> From: "Beauford"
>> To:
>> Sent: Tuesday, January 16, 2007 10:59 AM
>> Subject: [PHP-WIN] Ereg problems
>>=20
>>=20
>> > Hi,
>> >
>> > Without getting into a long drawn out discussion, can=20
>> anyone show me a=20
>> > way to validate a string that is input from a form.
>> >
>> > I want to allow the following: a to z, A to Z, 0 to 9, and "_-'"=20
>> > (without the double quotes) and spaces. I have now been screwing=20
>> > around with this
>> for
>> > over 8 hours. There are other issues as well that has been=20
>> compoudning
>> this,
>> > but this would be a great start to solving my issue.
>> >
>> > Thanks
>> >
>> > --
>> > PHP Windows Mailing List (http://www.php.net/) To=20
>> unsubscribe, visit:=20
>> > http://www.php.net/unsub.php
>> >
>>=20
>>=20
>>=20
>=20
> --=20
> PHP Windows Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>=20
>
------=_NextPart_000_004C_01C7398F.A3E68A90--
RE: Ereg problems
am 16.01.2007 18:59:56 von Beauford
No, this is what I said in my original email.
I want to allow the following: a to z, A to Z, 0 to 9, and "_-'" (without
the double quotes) and spaces.
I don't care about length, I don't care about order. I just want the user to
be able to use these characters. A to z A to Z 0 to 9 and the character _-'
and spaces.
So ABCDE __-' is valid, so is _-aaaBBcD' __
Thanks
> -----Original Message-----
> From: Niel Archer [mailto:Niel Archer] On Behalf Of Niel Archer
> Sent: January 16, 2007 11:46 AM
> To: php-windows@lists.php.net
> Subject: Re: [PHP-WIN] Ereg problems
>
> Hi
>
> What you've written doesn't match the description you gave.
> As a regular expression "([0-9][A-Z][a-z][-_.'\ ])" means a
> digit followed by an uppercase letter followed by a lower
> case letter followed by (hyphen or underscore or mid-dot or
> single quote or space). This is a total of four characters only.
> Is your string only supposed to be four characters in that format?
> I had assumed from your original post, that you wanted a
> variable length string, that could only contain alphanumeric
> characters plus hyphen, underscore, and single quote (I
> missed the space). All of which in any position. If this is
> not the case please describe exactly what it is you're trying
> to achieve.
>
>
>
> Niel
>
> --
> PHP Windows Mailing List (http://www.php.net/) To
> unsubscribe, visit: http://www.php.net/unsub.php
>
>
>
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Ereg problems
am 16.01.2007 19:49:36 von Daniel Anderson
Then I think this will do the trick for you :)
------------------------------------------------------------ --------------------
$string = "~YOUR STRING~"
if (!eregi_replace("[0-9A-Za-z -_']","",$string))
{
echo "string allowed";
}
else {
echo "string disallowed";
}
------------------------------------------------------------ --------------------
Warmest Regards,
Dan
----- Original Message -----
From: "Beauford"
To:
Sent: Tuesday, January 16, 2007 5:59 PM
Subject: RE: [PHP-WIN] Ereg problems
> No, this is what I said in my original email.
>
> I want to allow the following: a to z, A to Z, 0 to 9, and "_-'" (without
> the double quotes) and spaces.
>
> I don't care about length, I don't care about order. I just want the user
> to
> be able to use these characters. A to z A to Z 0 to 9 and the character
> _-'
> and spaces.
>
> So ABCDE __-' is valid, so is _-aaaBBcD' __
>
> Thanks
>
>> -----Original Message-----
>> From: Niel Archer [mailto:Niel Archer] On Behalf Of Niel Archer
>> Sent: January 16, 2007 11:46 AM
>> To: php-windows@lists.php.net
>> Subject: Re: [PHP-WIN] Ereg problems
>>
>> Hi
>>
>> What you've written doesn't match the description you gave.
>> As a regular expression "([0-9][A-Z][a-z][-_.'\ ])" means a
>> digit followed by an uppercase letter followed by a lower
>> case letter followed by (hyphen or underscore or mid-dot or
>> single quote or space). This is a total of four characters only.
>> Is your string only supposed to be four characters in that format?
>> I had assumed from your original post, that you wanted a
>> variable length string, that could only contain alphanumeric
>> characters plus hyphen, underscore, and single quote (I
>> missed the space). All of which in any position. If this is
>> not the case please describe exactly what it is you're trying
>> to achieve.
>>
>>
>>
>> Niel
>>
>> --
>> PHP Windows Mailing List (http://www.php.net/) To
>> unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>>
>
> --
> PHP Windows Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Ereg problems
am 16.01.2007 21:37:07 von Niel Archer
Hi
As I said before I don't use posix regexes (i.e. ereg), as they're not
binary safe and can be much slower than the PCRE versions. the PCRE
equivalent of what you want is:
if (preg_match("/^[-A-Za-z0-9_' ]+$/", $string)}:
echo "string valid";
else:
echo "string invalid";
endif;
The "A-Za-z0-9_" can be simplified to "\w" meaning any 'word'
character.
The slashes '/' are required as delimeters around a regex with PCRE.
The '^' and '$' anchor to beginning and end. This makes sure you're
testing the whole string not just a part of it, otherwise it would match
up to the first invalid character but still make a match.
I use this kind of test frequently myself, so I know it is possible
and fairly simple to do.
Niel
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php