5 Replies Latest reply on Jun 9, 2006 11:43 AM by Newsgroup_User

    Reg Expression

    bigj9901 Level 1
      Can someone show me a regular expression that would remove everything thats not a number from a string?

      Thanks
      Jeremy
        • 1. Re: Reg Expression
          Level 7
          bigj9901 wrote:
          > Can someone show me a regular expression that would remove everything thats not a number from a string?

          \D

          How do you want to use it? In Dreamweaver Find and Replace? In PHP? In
          some other language. Whichever you use \D identifies a non-number.
          That's all you need.

          --
          David Powers
          Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
          Author, "Foundation PHP 5 for Flash" (friends of ED)
          http://foundationphp.com/
          • 2. Re: Reg Expression
            bigj9901 Level 1
            I will be using it in a php function, not that good with reg expresions though. Could you give me some insight on how to filter a string and return only the numbers?

            Thanks
            • 3. Re: Reg Expression
              Level 7
              bigj9901 wrote:
              > I will be using it in a php function, not that good with reg expresions though. Could you give me some insight on how to filter a string and return only the numbers?

              Use preg_replace like this:

              <?php
              $string = 'ab34dfg843c3!|?8d4#@';
              $numbers = preg_replace('/\D/', '', $string);
              echo $numbers; // displays 34843384
              ?>

              '/\D/' is the regular expression. The second argument is a pair of
              quotes with nothing in between (an empty string). The effect is to
              replace all non-numbers with nothing.

              --
              David Powers
              Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
              Author, "Foundation PHP 5 for Flash" (friends of ED)
              http://foundationphp.com/
              • 4. Re: Reg Expression
                bigj9901 Level 1
                Just what i was looking for thanks. And thanks for explaining it too.
                • 5. Re: Reg Expression
                  Level 7
                  bigj9901 wrote:
                  > Just what i was looking for thanks. And thanks for explaining it too.

                  Glad to have been of help.

                  --
                  David Powers
                  Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
                  Author, "Foundation PHP 5 for Flash" (friends of ED)
                  http://foundationphp.com/