Parsing a string that changes in size

Posted on 2006-05-15
Last Modified: 2012-05-05
If I have a URL that looks like this:

And I want to return just the 515 value, how can I do that?  I tried a combination of parse_url and substr, but my problem is that the numeric value (as well as the path) can vary in size and value.

Question by:jjfbuy
    LVL 16

    Expert Comment

    $str= "";
    $str = preg_replace('/[^\d\/]/','',$str);
    echo $str;

    this code will isolate the file (any thing after the last / ) and then return only the number part of it
    LVL 49

    Expert Comment

    Like dr_dedo, put without the substr:

    $url= "";
    $filename = basename($url);
    $number  = preg_replace('/\D/', '', $filename);

    LVL 5

    Accepted Solution

    The provided solutions will return 1) any digits after the last '/', or, 2) any digits in the entire URL. This may or may not be what you need. In particular, Roonaan's solution will be susceptible to any digits that may exist in the site name, "product-cat", or "some-kind-of-product".

    There are a number of ways to match what you need, differing with the URL specifications: what changes, what remains the same, what kind of characters the product category or name will contain, etc.


    $url = '';

    If you just want to match any digit following the phrase '/ID-', you could do:

    $number = (preg_match('/\/ID-(\d+)/', $url, $m) ? $m[1] : FALSE);

    Getting a little more specific, this matches /ID-[digits]-*.htm, occuring at the end of the URL:

    $number = (preg_match('/\/ID-(\d+)-.+?\.htm$/', $url, $m) ? $m[1] : FALSE);

    Etc., etc.
    LVL 49

    Expert Comment


    Aminerd provided the most complete solution. Both dr_dedo's and mine are inaccurate.

    LVL 14

    Expert Comment

    Thanks a lot for your notification Roonan. I really appreciate your taking time and commenting on my decision. I will make a correction to my desicion in the next pass.
    Best regards,

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    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.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    745 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

    18 Experts available now in Live!

    Get 1:1 Help Now