0

我无法找出一种算法来检测 url 列表中重复出现的目录模式,任何人都可以为此建议一种方法吗?我很确定这将需要一个递归调用,但我无法决定如何为每种可能的模式保存记录。

注意:这是在 PHP 中。

以免说您有一些网址:

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

搜索一个目录的示例:

$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功能,请添加explodefor$name和比较 for $key,如果 dir is aaa/bbb/ccc,则检查index 0is 'aaa' 并且 on index 1isbbb和 on index 2is ccc,除非移动指针index+1并再次检查。

我希望我有所帮助。

于 2013-03-17T01:58:45.453 回答