Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to extract a number from a string in PHP ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 263
    Comment on it

    Hi Reader's,
    Welcome to FindNerd, today we are going to discuss how to extract a number from a string in PHP ?

    If you just want to filter the numbers out, the easiest is to use preg_replace() function.
    It is very simple and easy function to use.
    you can take reference of bellow example

    <?php
    //here define a variable of string
    $str = "Hi joe! 456 how Are You? 78";
    //here call preg_replace()
    $get_number = preg_replace('/[^0-9]+/', '', $str);
    //print $get_number variable
    print_r($get_number);
    
    ?>
    

    output will come following of above example

    45678
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: