?
Solved

Parsing a string that changes in size

Posted on 2006-05-15
7
Medium Priority
?
185 Views
Last Modified: 2012-05-05
If I have a URL that looks like this:

http://www.abcdefghijklm.com/product-cat/some-kind-of-product/ID-515-M-MAR.htm

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.

Thanks.
0
Comment
Question by:jjfbuy
5 Comments
 
LVL 16

Expert Comment

by:dr_dedo
ID: 16687215
<?
$str= "http://www.abcdefghijklm.com/product-cat/some-kind-of-product/ID-515-M-MAR.htm";
$str=substr($str,strrpos($str,'/')+1);
$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
0
 
LVL 49

Expert Comment

by:Roonaan
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);

-r-
0
 
LVL 5

Accepted Solution

by:
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.

Assuming...

$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.
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16881785
Hi,

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

-r-
0
 
LVL 14

Expert Comment

by:huji
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,
Huji
0

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