is there a quick way to remove non numeric chars

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chris

    #1

    is there a quick way to remove non numeric chars

    is there a quick and easy way to check a string for non numeric characters ?

    i have thought of making a routine to check each character to confirm it is
    0 - 9 but was wondering if there was a quicker way???


  • Michael Fesser

    #2
    Re: is there a quick way to remove non numeric chars

    .oO(chris)
    [color=blue]
    >is there a quick and easy way to check a string for non numeric characters ?
    >
    >i have thought of making a routine to check each character to confirm it is
    >0 - 9 but was wondering if there was a quicker way???[/color]

    For example regular expressions:

    $isNonNumeric = preg_match('#[^\d]#', $yourString);

    Micha

    Comment

    • chris

      #3
      Re: is there a quick way to remove non numeric chars

      that didnt seem to work But it gives me something to go on with

      thanks

      "Michael Fesser" <netizen@gmx.ne t> wrote in message
      news:t66pq013dr djjfteq93qsnt2v e2683gel7@4ax.c om...[color=blue]
      > .oO(chris)
      >[color=green]
      >>is there a quick and easy way to check a string for non numeric characters
      >>?
      >>
      >>i have thought of making a routine to check each character to confirm it
      >>is
      >>0 - 9 but was wondering if there was a quicker way???[/color]
      >
      > For example regular expressions:
      >
      > $isNonNumeric = preg_match('#[^\d]#', $yourString);
      >
      > Micha[/color]


      Comment

      • Hilarion

        #4
        Re: is there a quick way to remove non numeric chars

        > is there a quick and easy way to check a string for non numeric characters ?[color=blue]
        >
        > i have thought of making a routine to check each character to confirm it is 0 - 9 but was wondering if there was a quicker way???[/color]

        Do you want to only check if there are non-numeric chars (as stated in message
        body), or remove them (as stated in message subject)?
        If only check, then use "is_numeric " function. If replace, then you can
        use some regular expressions replacement like:

        $result = ereg_replace( '[^0-9]+', '', $source );


        Hilarion


        Comment

        • Daedalus

          #5
          Re: is there a quick way to remove non numeric chars

          > $result = ereg_replace( '[^0-9]+', '', $source );[color=blue]
          >[/color]

          From the manual page:
          "Note: preg_replace(), which uses a Perl-compatible regular expression
          syntax, is often a faster alternative to ereg_replace(). "


          Comment

          • Peter

            #6
            Re: is there a quick way to remove non numeric chars

            try this

            <?
            $str="222223433 934";

            if (ctype_digit($s tr))
            {
            print "The string is all digits";
            }
            else
            {
            print "the string has one or more characters that are not digits";
            }
            ?>


            chris wrote:[color=blue]
            > that didnt seem to work But it gives me something to go on with
            >
            > thanks
            >
            > "Michael Fesser" <netizen@gmx.ne t> wrote in message
            > news:t66pq013dr djjfteq93qsnt2v e2683gel7@4ax.c om...
            >[color=green]
            >>.oO(chris)
            >>
            >>[color=darkred]
            >>>is there a quick and easy way to check a string for non numeric characters
            >>>?
            >>>
            >>>i have thought of making a routine to check each character to confirm it
            >>>is
            >>>0 - 9 but was wondering if there was a quicker way???[/color]
            >>
            >>For example regular expressions:
            >>
            >>$isNonNumer ic = preg_match('#[^\d]#', $yourString);
            >>
            >>Micha[/color]
            >
            >
            >[/color]

            Comment

            • Hilarion

              #7
              Re: is there a quick way to remove non numeric chars

              > "Note: preg_replace(), which uses a Perl-compatible regular expression[color=blue]
              > syntax, is often a faster alternative to ereg_replace(). "[/color]

              That's true (but irrelevant if we are talking about user input validation).
              And AFAIK preg functions are binary safe, where ereg functions are not
              (which may be much more important).

              But I do not know Perl regular expressions syntax, and personaly think that
              posix syntax is easier to read.

              Hilarion


              Comment

              • Kelv

                #8
                Re: is there a quick way to remove non numeric chars

                chris wrote:[color=blue]
                > is there a quick and easy way to check a string for non numeric characters ?
                >
                > i have thought of making a routine to check each character to confirm it is
                > 0 - 9 but was wondering if there was a quicker way???[/color]

                preg_match("%[^0-9]%",$string);

                If there's anything other than 0-9 in $string, the condition returns true.

                Kelv :)

                --
                LoHost
                LoHost - Reliable low cost web hosting with PHP4 and PHP5, MySQL 4 database, htaccess creator and Zend Optimizer. Secure email hosting with authenticated SMTP, IMAP email hosting, webmail, spam blocker, virus blocker, subdomains and vacation. UK hosting, USA hosting, secure hosting, cart hosting, blog hosting, nightly backups, raid servers and more.


                Comment

                • Michael Fesser

                  #9
                  Re: is there a quick way to remove non numeric chars

                  .oO(Hilarion)
                  [color=blue]
                  >But I do not know Perl regular expressions syntax, and personaly think that
                  >posix syntax is easier to read.[/color]

                  The basic syntax is the same, except for the delimiters, which are
                  necessary in PCRE. Anything else are extensions, which make PCRE much
                  more flexible and powerful.

                  Micha

                  Comment

                  • d

                    #10
                    Re: is there a quick way to remove non numeric chars

                    No-one reads the PHP manual here it seems ;) There is a built-in function
                    for doing this:

                    if (ctype_digit($s tring)) {
                    // $string contains only numbers, no other characters
                    }

                    "chris" <someone@here.c om> wrote in message
                    news:41ac97c6$1 @funnel.arach.n et.au...[color=blue]
                    > is there a quick and easy way to check a string for non numeric characters
                    > ?
                    >
                    > i have thought of making a routine to check each character to confirm it
                    > is 0 - 9 but was wondering if there was a quicker way???
                    >[/color]


                    Comment

                    Working...