1

URL 内の 2 つの数値を解析しようとしています。URL は次のとおりです。

http://movies.actionpaxed.com/5600_5949/5943/5/pics/none/500k/3min/003.jpg?nvb=20130811232301&nva=20130812012301&hash=090a687f7e27b2f5ef735

URL の「5943/5」部分のみを取得しようとしています。URL を解析してから str_replace を使用するだけですが、必要な 2 つのフォルダーの名前が異なります。

これまでのところ、私は持っています:

$homepage = file_get_contents($url);
$link = parse_to_string('"video_url":"', '"};', $homepage);
$link = str_replace(array( '"low":"', '"};'), '', $link);
$link = utf8_decode(urldecode($link));

このコードの最後に $link = http://movies.actionpaxed.com/5600_5949/5943/5/pics/none/500k/3min/003.jpg?nvb=20130811232301&nva=20130812012301&hash=090a687f7e27b2f5ef735

私のためにこれを処理できる正規表現の助けがあれば、大歓迎です!

4

4 に答える 4

4

どうですか:

$res = explode('/', parse_url($url, PHP_URL_PATH));
$res = $res[2].'/'.$res[3];
echo $res;

デモ!

于 2013-08-12T01:18:23.550 に答える
0

これは、探しているものを抽出する関数です。

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 に採用しやすくなります。

于 2013-08-12T02:32:20.303 に答える