By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,745 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

How can I know if a string is alphanumeric?

P: n/a

Hi,

How can I know if a string only has alfanumeric chars?

Thanks
Jan 21 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
NC
Fernando Rodríguez wrote:

How can I know if a string only has alfanumeric chars?


By feeding it to is_numeric():

http://www.php.net/is_numeric

Cheers,
NC

Jan 21 '06 #2

P: n/a
NC
NC wrote:
Fernando Rodríguez wrote:

How can I know if a string only has alfanumeric chars?


By feeding it to is_numeric():


Oops, sorry, didn't read the question carefully...

"Alphanumeric" is a context-dependent concept. For example, ASCII 247
is not alphanumeric in Latin-1, but it does in fact correspond to a
letter in Cyrillic-1251...

What you may want to do is to define "alphanumeric" more precisely and
then look at each character in the sting to see if it meets your
definition of "alphanumeric".

Cheers,
NC

Jan 21 '06 #3

P: n/a
Fernando Rodredguez wrote:

Hi,

How can I know if a string only has alfanumeric chars?

Thanks


Use a regex.

C.
Jan 22 '06 #4

P: n/a

"Colin McKinnon"
<co**********************@ntlworld.deletemeunlessU RaBot.com> wrote in
message news:74*******************@newsfe3-gui.ntli.net...
Fernando Rodredguez wrote:

Hi,

How can I know if a string only has alfanumeric chars?

Thanks


Use a regex.

C.


give him an example!

preg_match('/^[\w\d]$/', $string);
Feb 9 '06 #5

P: n/a

<us****@isotopeREEMOOVEmedia.com> wrote in message
news:98********************************@4ax.com...
On Thu, 9 Feb 2006 14:02:04 -0800, "Jim Michaels"
<jm******@nospam.yahoo.com>
wrote:
"Colin McKinnon"
<co**********************@ntlworld.deletemeunles sURaBot.com> wrote in
message news:74*******************@newsfe3-gui.ntli.net...
Fernando Rodredguez wrote:

How can I know if a string only has alfanumeric chars?
Use a regex.

C.


give him an example!

preg_match('/^[\w\d]$/', $string);


Or use the built-in function.
www.php.net/ctype_alpha


that's half of it. I think you meant www.php.net/ctype_alnum
admittedly, this looks much easier. :-)
Feb 15 '06 #6

P: n/a
excellent function :D

Feb 15 '06 #7

P: n/a
On Thu, 9 Feb 2006 14:02:04 -0800, "Jim Michaels" <jm******@nospam.yahoo.com>
wrote:
How can I know if a string only has alfanumeric chars?


give him an example!

preg_match('/^[\w\d]$/', $string);


\w includes more than alphanumeric (it includes underscore), but includes all
of \d.

Alternatives could be:

/^[a-zA-Z\d]+$/

... or even ...

/^[^\W_]+$/

... for ASCII definitions of "alphanumeric", anyway.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Feb 15 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.