Forum Moderators: coopster

Message Too Old, No Replies

checking for % single with eregi function

         

brendan3eb

2:31 am on Mar 23, 2005 (gmt 0)

10+ Year Member



I'm having trouble detecting the percent sign with the eregi function, this is my current code:

if(eregi('^[0-9a-z%]+$', $username))

could anyone help me correct the syntax?

ironik

4:01 am on Mar 23, 2005 (gmt 0)

10+ Year Member



Not sure about ereg(), but if you are checking to see whether a username contains the % character I'd write it like this (with the preg_match() function)


$username = 'testuser%1';
if (preg_match('/^([\w]*%[\w]*)$/i', $username))
{
echo 'has percentage';
}

That checks for a percentage sign anywhere in the string. Is that what you were trying to achieve?

ironik

4:13 am on Mar 23, 2005 (gmt 0)

10+ Year Member



actually... scratch that, if you need to test for the existence of a % character then use:


$hasPercent = strpos('%', $username);
if ($hasPercent!== false)
{
echo 'has percentage character';
}

strpos is much faster that using a regular expression and easier to read... it just depends if there are other factors you need to test for in the same username (has to have a certain number of words etc.)

brendan3eb

1:57 am on Mar 26, 2005 (gmt 0)

10+ Year Member



thanks very much. That is a great concept for other characters too, this thread is going in my favorites :)