regular expressions

Posted on 2007-07-20
Last Modified: 2008-02-26
$var = string101;
$var = string1012;
$var = string10123;

hi how can i use regular expression to retrieve only the numbers from this variable in php
Question by:woleraymond
    1 Comment
    LVL 42

    Accepted Solution

    use preg_match

    $var1 = string101;
    $var2 = string1012;
    $var3 = string10123;

    $pattern = '/[0-9]+/';
    preg_match($pattern, $var1, $matches);

    $num1 = $matches[0];


    this would have $num1 = 101;

    you could repeat for the others

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now