Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    Oregon
    Posts
    243
    Thanks
    23
    Thanked 10 Times in 10 Posts

    echo numeric characters ONLY in a string?

    Could someone help with this?

    for this line of text charactors and numbers;
    Company (100)

    how would I write code to echo "only numeric values";

    result = 100

  • #2
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Try this out.
    PHP Code:
    $str "int(11)";
    print 
    preg_replace("/([^0-9]+)/","",$str); 
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Or:
    PHP Code:
    // remove non-numeric characters
    $string preg_replace'/\D/'''$string ); 

  • #4
    Regular Coder
    Join Date
    Nov 2010
    Location
    Oregon
    Posts
    243
    Thanks
    23
    Thanked 10 Times in 10 Posts
    Thanks DJCMBear! your suggestion worked in my example....

    kbluhm: not sure how your suggestion would apply to this, and will that code remove any and all chatacters other other than numeric?

    Code:
    $str = $columns[Type];
    $size = preg_replace("/([^0-9]+)/","",$str);
    echo $size;

  • #5
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Quote Originally Posted by kbluhm View Post
    Or:
    PHP Code:
    // remove non-numeric characters
    $string preg_replace'/\D/'''$string ); 
    Wouldn't it be this, saying if now digit?
    As currently it looks like it's setting all digits to nothing, not 100% sure though.
    PHP Code:
    // remove non-numeric characters
    $string preg_replace'/^\D/'''$string ); 
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    No, that's wrong. In the above example, the ^ signifies the beginning of the string in that context, so it will remove the first character from the beginning of the string... only if is a non-digit.

    \d is a digit, equivalent to [0-9]
    \D is a non-digit, equivalent to [^0-9]

    Just as:
    \w is a word character, equivalent to [A-Za-z0-9_]
    \W is a non-word character, equivalent to [^A-Za-z0-9_]
    \s is a whitespace character
    \S is a non-whitespace character, equivalent to [^\s]

    ...etc.

    So, yes DataTalk, the following code will remove all non-digits from the string:
    PHP Code:
    $string preg_replace'/\D/'''$string ); 
    The original suggestion will work, I just provided a simpler alternative. Learning the ins-and-outs of PCRE will take you a long way.
    Last edited by kbluhm; 12-27-2010 at 07:35 PM.

  • Users who have thanked kbluhm for this post:

    CloudWriter (12-28-2010)

  • #7
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Ahhhhh sorry I missed the caps lol my bad, I get what it's doing now lol.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P


  •  

    Posting Permissions

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