Parsing a string that changes in size

Posted on 2006-05-15
Medium Priority
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

ID: 16687215
$str= "http://www.abcdefghijklm.com/product-cat/some-kind-of-product/ID-515-M-MAR.htm";
$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

ID: 16687935
Like dr_dedo, put without the substr:

$url= "http://www.abcdefghijklm.com/product-cat/some-kind-of-product/ID-515-M-MAR.htm";
$filename = basename($url);
$number  = preg_replace('/\D/', '', $filename);


Accepted Solution

aminerd earned 2000 total points
ID: 16694562
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 = 'http://www.abcdefghijklm.com/product-cat/some-kind-of-product/ID-515-M-MAR.htm';

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

ID: 16881785

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

LVL 14

Expert Comment

ID: 16881803
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …
Suggested Courses
Course of the Month17 days, 13 hours left to enroll

831 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