【问题标题】:PHP url array search and return closest urlPHP url数组搜索并返回最近的url
【发布时间】:2018-03-14 03:04:47
【问题描述】:

我有以下带有 url 的数组

$data = Array ( 'http://localhost/my_system/users',
        'http://localhost/my_system/users/add_user', 
        'http://localhost/my_system/users/groups',
        'http://localhost/my_system/users/add_group' );

然后我有一个变量

$url = 'http://localhost/my_system/users/by_letter/s';

如果 $url 不存在,我需要一个函数来返回数组中最近的 url。像

function get_closest_url($url,$data){

}



get_closest_url($url,$data); //returns 'http://localhost/my_system/users/'

$url2 = 'http://localhost/my_system/users/groups/ungrouped';

get_closest_url($url2,$data); //returns 'http://localhost/my_system/users/groups/'

$url3 = 'http://localhost/my_system/users/groups/add_group/x/y/z';

get_closest_url($url3,$data); //returns 'http://localhost/my_system/users/groups/add_group/'

【问题讨论】:

  • 那些不是字符串。每行末尾的; 在哪里?
  • 你能修正你的代码格式吗?
  • 那些网址怎么样:localhost/somethinglocalhost/my_systems/no_users。它们没有出现在您的列表中,那么在这些情况下应该返回什么函数?
  • 我怀疑您需要使用 'curl_*()' 或类似 'file_get_contents() 函数。您的网址可以由网络服务器(apache / nginx / etc)重写,如果没有实际的“ping”网址就无法检测......但是检查每个不存在的路径会相对较长...... imo

标签: php arrays string substring


【解决方案1】:
function get_closest_url($item,$possibilities){
    $result = [];
    foreach($possibilities as $possibility){
        $lev = levenshtein($possibility, $item);
        if($lev === 0){
            #### we have got an exact match
            return $possibility;
        }
        #### if two possibilities have the same lev we return only one
        $result[$lev] = $possibility;
    }
    #### return the highest
    return $result[min(array_keys($result))];
}

应该可以的。

【讨论】:

【解决方案2】:

您可以分解当前 URL 和 $data 中的每个 URL,将数组相交,然后返回具有最多元素的数组(最佳匹配)。如果没有匹配,返回false:

<?php
$data = [ "localhost/my_system/users",
        "localhost/my_system/users/add_user", 
        "localhost/my_system/users/by_letter/groups",
        "localhost/my_system/users/add_group"];
$url = "localhost/my_system/users/by_letter/s";
function getClosestURL($url, $data) {
    $matches = [];
    $explodedURL = explode("/", $url);
    foreach ($data as $match) {
        $explodedMatch = explode("/", $match);
        $matches[] = array_intersect($explodedMatch, $explodedURL);
    }
    $bestMatch = max($matches);
    return count($bestMatch) > 0 ? implode("/", $bestMatch) : false; // only return the path if there are matches, otherwise false
}

var_dump(getClosestURL($url, $data)); //returns localhost/my_system/users/by_letter
var_dump(getClosestURL("local/no/match", $data)); //returns false

Demo

您没有提到要如何专门检查 URL 是否存在。如果需要“实时”,您可以使用get_headers() 并检查第一项的HTTP 状态。如果不是200,则可以继续进行 URL 交集。

$headers = get_headers($url);
$httpStatus = substr($headers[0], 9, 3);
if ($httpStatus === "200") {
    return $url; // $url is OK
}
// else, keep going with the previous function

【讨论】:

    猜你喜欢
    • 2016-09-06
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    相关资源
    最近更新 更多