You can get the identifier using a combination of dirname()
and basename()
:
$url = 'http://cdn.blah.com/mp4/IDENTIFIER/somefile.mp4';
$identifier = basename(dirname($url));
Frage
I've got a PHP script that does batch wgets from a database, but need to create a mechanism to generate folders so I can loop it all to save each file in its own folder...
Can one use regex to find the IDENTIFIER (which can be anything/any length, sometimes with underscores, sometimes with hyphens other times not) in following URL or is there some other way? I have no idea about regex...
http://cdn.blah.com/mp4/IDENTIFIER/somefile.mp4
For example, can one find IDENTIFIER and return it as a string? That way I can pass it to write "wget -O /somedir/" . $IDENTIFIER . "/"; and that would help greatly!
Any help very appreciated. Thanks!
Lösung
You can get the identifier using a combination of dirname()
and basename()
:
$url = 'http://cdn.blah.com/mp4/IDENTIFIER/somefile.mp4';
$identifier = basename(dirname($url));
Andere Tipps
I dunno anything about PHP but I notice that
/.*\/([^/]+)\/[^/]+/.exec(s)
pops IDENTIFIER right out for you.
edit: a very brief Google suggests the PHP equivalent would be
<?php
$subject = "http://cdn.blah.com/mp4/IDENTIFIER/somefile.mp4";
$pattern = "/.*\/([^/]+)\/[^/]+/";
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>