0

URLのリストで繰り返し発生するディレクトリパターンを検出するアルゴリズムを理解するのに問題があります。誰かがこの方法を提案できますか?再帰呼び出しである必要があると確信していますが、考えられる各パターンのレコードを保持する方法を決定することはできません。

:これはPHPです。

レスはあなたがいくつかのURLを持っていると言います:

1. http://www.goodfood.com/recipes/special_occasion/desserts/pie/chocolate-pie.html
2. http://www.goodfood.com/recipes/special_occasion/desserts/pie/cherry-pie.html
3. http://www.goodfood.com/recipes/special_occasion/apps/chex-mix.html
4. http://www.goodfood.com/recipes/special_occasion/soup/tomato.html
5. http://www.goodfood.com/special/special_occasion/soup/beef-stew.html
6. http://www.goodfood.com/special/special_occasion/soup/vegetable.html

複数のURLが持つ可能性のあるすべてのディレクトリパターンを特定する方法を見つけたいと思います。したがって、結果は次のようになります。

'recipes/special_occasion' is found in urls 1, 2, 3 and 4.
'recipes/special_occasion/desserts' is found in urls 1, and 2.
'recipes/special_occasion/desserts/pie' is found in urls 1, and 2.
'special_occasion/desserts/pie' is found in urls 1, and 2.
'desserts/pie' is found in urls 1, and 2.
'special_occasion/desserts' is found in urls 1, and 2.
'special_occasion/desserts/pie' is found in urls 1, and 2.
'special/special_occasion' is found in urls 5, and 6.
'special/special_occasion/soup' is found in urls 5, and 6.
'special_occasion/soup' is found in urls 5, and 6.

私の考えは、各URLを調べて、考えられるすべての新しいパターンを引き出し、配列に格納することです。これまでのところ、次のようになっています。$ commonDomains = array();

     foreach($query AS $row) {


         $urlPath = parse_url($row['href'], PHP_URL_PATH);
         echo "$urlPath<br/>";

         $urlChunks = explode('/', $urlPath);
         //var_dump($urlChunks);

         foreach($urlChunks AS $domain) {
             if(strlen($domain) > 0) {
                $thisDomain = $domain.'/';
                $commonDomains[$thisDomain][] = $row['id'];
             }
         }
         var_dump($commonDomains);
     }

誰かがこれに遭遇したことがありますか?それは私にパターンを叫びますが、私はまだオンラインで答えを見つけることができません。私が考えるすべては非常に速く非常に複雑になります。助けてください、ありがとう。


私が取り組んでいることの例があります:http://phpfiddle.org/main/code/kn4-zyh

これまでの私の結果はここにあります

/recipes/special_occasion/desserts/pie/grandmas-chocolate-pie.html
array(5) { [0]=> string(7) "recipes" [1]=> string(16) "special_occasion" [2]=> string(8) "desserts" [3]=> string(3) "pie" [4]=> string(27) "grandmas-chocolate-pie.html" } 

0 : 4 : recipes/special_occasion/desserts/pie/grandmas-chocolate-pie.html
0 : 3 : recipes/special_occasion/desserts/pie
0 : 2 : recipes/special_occasion/desserts
0 : 1 : recipes/special_occasion

1 : 4 : special_occasion/desserts/pie/grandmas-chocolate-pie.html
2 : 4 : desserts/pie/grandmas-chocolate-pie.html
3 : 4 : pie/grandmas-chocolate-pie.html

0 : 4 : recipes/special_occasion/desserts/pie/grandmas-chocolate-pie.html
1 : 3 : special_occasion/desserts/pie


**Im missing:
2 : 3 : special_occasion/desserts
1 : 2 : recipes/special_occasion

**

4

1 に答える 1

0

1つのディレクトリの検索例:

$links = array(
    'http://www.goodfood.com/recipes/special_occasion/desserts/pie/chocolate-pie.html',
    'http://www.goodfood.com/recipes/special_occasion/desserts/pie/cherry-pie.html',
    'http://www.goodfood.com/recipes/special_occasion/apps/chex-mix.html',
    'http://www.goodfood.com/recipes/special_occasion/soup/tomato.html',
    'http://www.goodfood.com/special/special_occasion/soup/beef-stew.html',
    'http://www.goodfood.com/special/special_occasion/soup/vegetable.html',
);

$dirs = array();
foreach ($links as $key => $link) {
    $urlPath = parse_url($link, PHP_URL_PATH);
    $arrayUrlPath = explode('/', $urlPath);
    $dirs[$key] = array();
    $counter = 0;
    foreach ($arrayUrlPath as $dir) {
        if (empty($dir) || in_array(substr($dir, -5), array('.html'))) {
            continue;
        }
        $dirs[$key][$counter++] = $dir;
    }
}

$searchDirs = $dirs;

foreach ($searchDirs as $key => $dir) {
    foreach ($dir as $name) {
        echo 'dir: ' . $name . ', found in: ' . search($name, $key, $dirs) . "\n";
    }
}

function search($name, $excludeKey, $dirs)
{
    $return = array();
    foreach ($dirs as $key => $dir) {
        if ($key === $excludeKey) {
            continue;
        }
        if (in_array($name, $dir)) {
            $return[] = (int)$key + 1;
        }
    }
    return join(', ', $return);
}

より長い文字列の再構築関数を検索する場合はsearch、追加explode$nameて比較し$keyます。dirがaaa/bbb/ccc、の場合、ポインタを移動して再度チェックしない限り、オンindex 0は「aaa」で、オンindex 1bbbオンindex 2です。cccindex+1

お役に立てば幸いです。

于 2013-03-17T01:58:45.453 に答える