How can I know if a string is alphanumeric?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fernando Rodríguez

    How can I know if a string is alphanumeric?


    Hi,

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

    Thanks


  • NC

    #2
    Re: How can I know if a string is alphanumeric?

    Fernando Rodríguez wrote:[color=blue]
    >
    > How can I know if a string only has alfanumeric chars?[/color]

    By feeding it to is_numeric():

    Finds whether a variable is a number or a numeric string


    Cheers,
    NC

    Comment

    • NC

      #3
      Re: How can I know if a string is alphanumeric?

      NC wrote:[color=blue]
      > Fernando Rodríguez wrote:[color=green]
      > >
      > > How can I know if a string only has alfanumeric chars?[/color]
      >
      > By feeding it to is_numeric():[/color]

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

      "Alphanumer ic" 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 "alphanumer ic" more precisely and
      then look at each character in the sting to see if it meets your
      definition of "alphanumer ic".

      Cheers,
      NC

      Comment

      • Colin McKinnon

        #4
        Re: How can I know if a string is alphanumeric?

        Fernando Rodredguez wrote:
        [color=blue]
        >
        > Hi,
        >
        > How can I know if a string only has alfanumeric chars?
        >
        > Thanks[/color]

        Use a regex.

        C.

        Comment

        • Jim Michaels

          #5
          Re: How can I know if a string is alphanumeric?


          "Colin McKinnon"
          <colin.thisisno tmysurname@ntlw orld.deletemeun lessURaBot.com> wrote in
          message news:74NAf.6252 0$Dg6.12645@new sfe3-gui.ntli.net...[color=blue]
          > Fernando Rodredguez wrote:
          >[color=green]
          >>
          >> Hi,
          >>
          >> How can I know if a string only has alfanumeric chars?
          >>
          >> Thanks[/color]
          >
          > Use a regex.
          >
          > C.[/color]

          give him an example!

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


          Comment

          • Jim Michaels

            #6
            Re: How can I know if a string is alphanumeric?


            <usenet@isotope REEMOOVEmedia.c om> wrote in message
            news:98jsu1pkpu hasfudb7iksdp7h 75jbc3sns@4ax.c om...[color=blue]
            > On Thu, 9 Feb 2006 14:02:04 -0800, "Jim Michaels"
            > <jmichae3@nospa m.yahoo.com>
            > wrote:[color=green]
            >>"Colin McKinnon"
            >><colin.thisis notmysurname@nt lworld.deleteme unlessURaBot.co m> wrote in
            >>message news:74NAf.6252 0$Dg6.12645@new sfe3-gui.ntli.net...[color=darkred]
            >>> Fernando Rodredguez wrote:
            >>>
            >>>> How can I know if a string only has alfanumeric chars?
            >>>>
            >>>
            >>> Use a regex.
            >>>
            >>> C.[/color]
            >>
            >>give him an example!
            >>
            >>preg_match( '/^[\w\d]$/', $string);
            >>[/color]
            >
            > Or use the built-in function.
            > www.php.net/ctype_alpha[/color]

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


            Comment

            • Cmac

              #7
              Re: How can I know if a string is alphanumeric?

              excellent function :D

              Comment

              • Andy Hassall

                #8
                Re: How can I know if a string is alphanumeric?

                On Thu, 9 Feb 2006 14:02:04 -0800, "Jim Michaels" <jmichae3@nospa m.yahoo.com>
                wrote:
                [color=blue][color=green][color=darkred]
                >>> How can I know if a string only has alfanumeric chars?[/color][/color]
                >
                >give him an example!
                >
                >preg_match('/^[\w\d]$/', $string);[/color]

                \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 "alphanumer ic", anyway.

                --
                Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
                http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

                Comment

                Working...