0

I've got an array with URLs in it like so:

$urls = array(
    "http://myurl.com/file/2222/file.rar",
    "http://myurl.com/file/2222/file.part1.rar",
    "http://myurl.com/file/2222/file.part2.rar",
    "http://myurl/file/2222/file.part3.rar",
    "http://myurl.com/file/2222/file.part4.rar"
);

I want to find any of the filenames to check if they are a part or a single file.

For example the result for the above should be:

$single = "http://myurl.com/file/2222/file.rar";
$splits = array("http://myurl.com/file/2222/file.part1.rar", "http://myurl.com/file/2222/file.part2.rar", "http://myurl.com/file/2222/file.part3.rar", "http://myurl.com/file/2222/file.part4.rar");
3
  • Im thinking i need to use in_array somehow but i'm not entirely sure. Commented Dec 18, 2013 at 21:11
  • How easily can you predict the file name? Do they follow a standard convention? Commented Dec 18, 2013 at 21:12
  • Yes they are always either NAME.rar or NAME.part.rar so i need to get all the URLS with part into one array and all the urls without part into another array Commented Dec 18, 2013 at 21:13

1 Answer 1

2

I like preg_grep():

$parts = preg_grep('/\.part\d+\.rar/', $urls);
$singles = array_diff($urls, $parts);

Or to do the opposite for the singles:

$singles = preg_grep('/\.part\d+\.rar/', $urls, PREG_GREP_INVERT);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.