Results 1 to 5 of 5

Thread: Remove all non digit characters

  1. #1

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Remove all non digit characters

    How can I take a string and remove all non digit characters from the string? Unfortunately my regex skills arent so hot

  2. #2

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: Remove all non digit characters

    This seems to be working well:

    Code:
    	$str = "1234567890abc!@#$&#37;^&*()-=_+[]{}|\\'\"?/><,.";
    	echo preg_replace("%[^0-9]%", "", $str);
    Any objections?

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Remove all non digit characters

    No objections. You can use \d as well as 0-9.

  4. #4

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: Remove all non digit characters

    how do i turn this into a preg_match?

    I need to match if a string is all digits, if so true else false

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Remove all non digit characters

    PHP Code:
        if(preg_match('/^\d+$/'$str)){
          echo 
    "matched!\n";
        }else{
          echo 
    "no match.\n";
        } 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width