これは、探しているものを抽出する関数です。
function getTheStuff($url) {
// Only get the part of the URL that
// actually matters; this makes the
// problem smaller and easier to solve
$path = parse_url($url, PHP_URL_PATH);
// The path will be false if the URL is
// malformed, or null if it was not found
if ($path !== false && $path !== null) {
// Assuming that the stuff you need is
// always after the first forward slash,
// and that the format never changes,
// it should be easy to match
preg_match('/^\/[\d_]+\/(\d+\/\d+)/', $path, $result);
// We only capture one thing so what we
// are looking for can only be the second
// thing in the array
if (isset($result[1])) {
return $result[1];
}
}
// If it is not in the array then it
// means that it was not found
return false;
}
$url = 'http://movies.actionpaxed.com/5600_5949/5943/5/pics/none/500k/3min/003.jpg?nvb=20130811232301&nva=20130812012301&hash=090a687f7e27b2f5ef735';
var_dump(getTheStuff($url));
これを自分で書いていたら、正規表現を避けていたでしょう。この場合はこれが一番簡単なので、それを使用しました。おそらく、$path
(区切り文字として使用/
して) をトークン化することでソリューションを一般化し、別の関数/メソッド/メカニズムに必要な部分の抽出を処理させます。そうすれば、フォーマットが異なる他の URL に採用しやすくなります。