使用正则表达式在URL中查找IDENTIFIER并以字符串形式返回
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!
我有一个PHP脚本从数据库执行批处理wgets,但是需要创建一个生成文件夹的机制 所以我可以将它全部循环以将每个文件保存在自己的文件夹中... p>
可以使用正则表达式来查找IDENTIFIER(可以是任何/任何长度,有时带下划线,有时 在其他时间没有连字符)在以下网址中还是有其他方式吗? 我不知道正则表达式... p>
http ://cdn.blah.com/mp4/IDENTIFIER/somefile.mp4 p>
例如,可以找到IDENTIFIER并将其作为字符串返回吗? 这样我就可以传递它来写“wget -O / somedir /”。 $ IDENTIFIER。 “/”; 这将有很大帮助! p>
任何帮助非常感谢。 谢谢! p> div>
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));
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);
?>