Jump to content


Photo

quick PHP Array Help please


  • Please log in to reply
27 replies to this topic

#21 samshel

samshel
  • Members
  • PipPipPip
  • Advanced Member
  • 837 posts

Posted 01 June 2006 - 05:18 AM


if(eregi("[^a-zA-Z0-9]+",$str)) {
    echo "Invalid Chars !!";
} else {
    echo "All Valid Chars !!";
}

This should work.....
Cheers,
SamShel
-----------------------------------------------
--: FUTURE IS LINUX :--
-----------------------------------------------

#22 Old Novus

Old Novus
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 02 June 2006 - 01:41 AM

another bump...still not working....guys please look at the full script and youll be able to better see what were trying to accomplish. this is hard i guess.

#23 Old Novus

Old Novus
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 02 June 2006 - 02:19 AM

if (!eregi_match("/^\([0-9A-Za-z])+$/", $username)) {
echo '<script language="Javascript">alert ("Bad characters in username")</script>';
include("index.html");
exit();

This isnt working...we need it to ONLY allow a-z and 0-9 and nothing else...

#24 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 02 June 2006 - 04:48 AM

OK, I have a working one I guess. It's actually the same as I posted before, but with a minor typo corrected:

<?php

// Some entries to test
$test_array = array ('username', '21358', 'user2', 'l337z_cx', 'ccc*ccc', 'cr@p');

// Last Regex, OMG, forgot to remove the backslash! "\"!
// $old_regex = "/^\([0-9A-Za-z])+$/";

// This one shold do the job
$regex = "/^([A-Za-z0-9])+$/";

// Testing all entries
for ($i=0; $i<count($test_array); $i++) {
   echo $test_array[$i] . ': ';
   echo (!preg_match($regex, $test_array[$i])) ? 'Bad' : 'OK';
   echo '<br />';
}

?>

This will generate:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]username: OK
21358: OK
user2: OK
l337z_cx: Bad
ccc*ccc: Bad
cr@p: Bad[/quote]

So it's basically:

if ((!preg_match("/^([A-Za-z0-9])+$/", $username)) {
   echo '<script language="Javascript">alert ("Bad characters in username")</script>';
   include("index.html");
   exit();
} else {
   echo 'OK, good to go!';
}

Just to point something out, that regex above will return false for any string with characters other than letters and numbers. This includes the space.

So, to avoid annoyances, use the trim() function to remove leading/trailing spaces that are commonly mis-entered by users:

' accident' will return false.
But if you trim(' accident'), it will become 'accident' and therefore return true.
~ D Kuang

#25 Old Novus

Old Novus
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 03 June 2006 - 06:31 AM

Parse error: syntax error, unexpected '{' in C:\www\webroot\process.php on line 41

thats the nwe error...and its on the code line where preg is...on that line.

#26 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 June 2006 - 12:27 PM

There is either one two many parenthesis or one two few on this line:
<?php if ((!preg_match("/^([A-Za-z0-9])+$/", $username)) { ?>[/code]

BTW, why are you using a Javascript alert in this code? If you are verifying in PHP, use PHP to put out the error message. The Javascript alert will probably not display where you think it will.

Ken

#27 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 03 June 2006 - 02:03 PM

Most likely, it's what ken said. Or if it's not, post the code here.

Anyway, the alert will pop, but you shouldn't use it this way ;)
~ D Kuang

#28 Old Novus

Old Novus
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 04 June 2006 - 04:30 AM

you are god thank you so much!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users