#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    2

    Find the first number in a string?


    I need to split a string when it encounters a number.. and split everything after it .

    Explode doesn't work cause the number can change. any regular expression hero's ? thanks
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    PHP Code:
    <?php

    $res 
    preg_split('/\d+/''Split -->42<-- here 4'2);
    var_dump($res);
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    2
    Originally Posted by Jacques1
    PHP Code:
    <?php

    $res 
    preg_split('/\d+/''Split -->42<-- here 4'2);
    var_dump($res);
    Thank you for the fast reply! this works. But is it also possible to split and keep everything after that number?

    as in "Something 2euro" and i want to seperate the "something" and the "2euro"

    Regular expressions are so hard!
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    So you wanna keep the number? When you say "everything after the number", I read that as "number not included".

    PHP Code:
    <?php

    $res 
    preg_split('/(?=\d+)/''Something 2euro'2);
    var_dump($res);
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo