Forum Moderators: coopster

Message Too Old, No Replies

checking if string contains a-z 0-9

         

rokec

10:04 pm on Sep 9, 2006 (gmt 0)

10+ Year Member



Can you help me and write code which will detect if string contains just a-z A-Z 0-9?

spinnercee

12:24 am on Sep 10, 2006 (gmt 0)

10+ Year Member



Quick code:

/* This will strip $anystring of everything but A-Z,a-z,0-9 to create $newstring */

$anystring="Whatever you want it to be! --!@#$%^&*()";
$newstring="";

for ($i = 0; $i < strlen($anystring); $i++) {

/* step through the string... a case select() would be better... */

if ((ord($anystring[$i]) > 47) && (ord($anystring[$i]) < 58))
$newstring .= $anystring[$i]; // 0-9 [48-57]

if ((ord($anystring[$i]) > 64) && (ord($anystring[$i]) < 91))
$newstring .= $anystring[$i]; // A-Z [65-90]

if ((ord($anystring[$i]) > 96) && (ord($anystring[$i]) < 123))
$newstring .= $anystring[$i]; // a-z [97-122]

}

echo $newstring;

/* both strings will be the same size if the original string was OK, so... */

$isAllAlphaNumeric=FALSE;

if (strlen($newstring) == strlen($anystring))
$isAllAlphaNumeric=TRUE;


You can find an ASCII table here [lookuptables.com...]

A function from the PHP man pages that allows some punctuation...


function cleanstr($string){
$len = strlen($string);
for($a=0; $a<$len; $a++){
$p = ord($string[$a]);
# chr(32) is space, it is preserved..
(($p > 64 && $p < 123) ¦¦ $p == 32)? $ret .= $string[$a] : $ret .= "";
}
return $ret;
}

[edited by: spinnercee at 12:40 am (utc) on Sep. 10, 2006]

Little_G

12:38 am on Sep 10, 2006 (gmt 0)

10+ Year Member



or, you could use ctype_alnum [uk.php.net]

example:

if(ctype_alnum($string)){
echo "string contains only a-z A-Z 0-9";
}
else{
echo "string doesn't contain only a-z A-Z 0-9";
}

Andrew

Psychopsia

1:07 am on Sep 10, 2006 (gmt 0)

10+ Year Member




$str = 'textTEXT012345';

if (preg_match('#([a-zA-Z0-9]+)#is', $str))
{
// contains only a-z and 0-9
}
else
{
// show error or something else
}

Hope this helps :)

[edited by: Psychopsia at 1:08 am (utc) on Sep. 10, 2006]

rokec

4:41 am on Sep 10, 2006 (gmt 0)

10+ Year Member



Cool three different functions to meake the same thing.

dreamcatcher

7:28 am on Sep 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's also the character class:


if (ereg("^[[:alnum:]]+$", $string))
{
// ok
}
else
{
// not ok
}

dc

rokec

9:08 am on Sep 10, 2006 (gmt 0)

10+ Year Member



And now i want to include @ and _ also. What can i do?

Little_G

11:50 am on Sep 10, 2006 (gmt 0)

10+ Year Member



Hi,

If including @ and _ then preg_match will do the job best.
preg_match('/([a-zA-Z0-9@_]+)/', $str);

Andrew

rokec

8:22 am on Sep 23, 2006 (gmt 0)

10+ Year Member



And how can i do that it detects if JUST a-Z and 0-9 and _ are in the string?

Thanks for help!

Little_G

1:07 pm on Sep 23, 2006 (gmt 0)

10+ Year Member



Hi,

remove the @ character.

Andrew

rokec

5:02 pm on Sep 23, 2006 (gmt 0)

10+ Year Member



I know, but i want to check if string contains JUST thiese characters.
For example, this code accepts ert%ds also, but i don't want it.

Little_G

8:55 pm on Sep 23, 2006 (gmt 0)

10+ Year Member



Hi,

I see what you mean, try:

preg_match('/^([a-zA-Z0-9_]+)$/', $str);

Andrew

rokec

7:02 am on Sep 24, 2006 (gmt 0)

10+ Year Member



Thanks, this really works!