PHP regular expression

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nitinpatel1117
    New Member
    • Jun 2007
    • 111

    PHP regular expression

    Hi, I'm trying to do a regular expression, which I am pretty new to.

    What I am trying to do is remove all characters from a string that is not a number or a space;

    so for example the text: adsf69a4 65asfd
    should be outputed as: 694 65

    I have tried different combinations of this regular expression but can't get it to work properly.

    This is what i have so tried and various combinations:

    [PHP]$number = eregi_replace("[^0-9\s]", "", $number);[/PHP]

    [PHP]$number = eregi_replace("[^0-9][:space:]", "", $number);[/PHP]


    [PHP]$number = eregi_replace(" ([^0-9]|[^\s])", "", $number);[/PHP]


    any help on this would be great. Thanks.
  • nitinpatel1117
    New Member
    • Jun 2007
    • 111

    #2
    ok i figured it out, it is:

    [PHP]$number = eregi_replace("[^0-9[:space:]]", "", $number);[/PHP]

    Don't quite know why i have to use [:space:] and not \s

    Comment

    • henryrhenryr
      New Member
      • Jun 2007
      • 103

      #3
      Originally posted by nitinpatel1117
      Don't quite know why i have to use [:space:] and not \s
      probably the difference between "preg_repla ce" and "eregi_repl ace" the manual covers it I think

      Last edited by Atli; Jun 19 '08, 08:29 PM. Reason: Fixed the link.

      Comment

      • nashruddin
        New Member
        • Jun 2008
        • 25

        #4
        Or use preg_replace:

        Code:
        $number = preg_replace("/[^0-9\s]/i", "", $number);

        Comment

        Working...