Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

using a preg match

Posted on 2011-10-27
5
Medium Priority
?
306 Views
Last Modified: 2012-05-12
hello there,
I would like to know how can I use preg match.
I am trying to extract the image ID and the file name.. the issue is that the file name always changes with the ID..

samples:
http://site.com/image/89869027/pic1.rar
http://site.com/image/25856651/pic2.jpg
http://site.com/image/94546565/pic3.zip

how can I do this exactly?
0
Comment
Question by:XK8ER
  • 4
5 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 37042353

$url = "http://site.com/image/89869027/pic1.rar";
preg_match("@(\d+)/([^/]+)$@", $url, $matches);
unset($matches[0]);
print_r($matches);

Output:
Array
(
    [1] => 89869027
    [2] => pic1.rar
)

Open in new window

0
 
LVL 1

Author Comment

by:XK8ER
ID: 37042359
how can I set the matches in separate arrays for example..

arrayFiles[];
and
arrayIDs[];
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 37042370

$url = "http://site.com/image/89869027/pic1.rar";
if (preg_match("@(\d+)/([^/]+)$@", $url, $matches)) {
  arrayIDs[] = $matches[1];
  arrayFiles[] = $matches[2];
}

Open in new window

0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 total points
ID: 37042372
Oops - forgot the $ on the array variables
$url = "http://site.com/image/89869027/pic1.rar";
if (preg_match("@(\d+)/([^/]+)$@", $url, $matches)) {
  $arrayIDs[] = $matches[1];
  $arrayFiles[] = $matches[2];
}

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 37042377
That code assumes you've just got one URL and nothing else in your source string, and there's no query string on the end.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
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.
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

581 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