假如我有如下字符串
(
    [0] => http://news.qq.com/newsgn/zhxw/shizhengxinwen_2.htm
    [1] => http://news.qq.com/newsgn/zhxw/shizhengxinwen_3.htm
    [2] => http://news.qq.com/newsgn/zhxw/shizhengxinwen_4.htm
    [3] => http://news.qq.com/newsgn/zhxw/shizhengxinwen_5.htm
    [4] => http://news.qq.com/newsgn/zhxw/shizhengxinwen_80.htm
)有什么好的算法可以得到他们的不同点,用(*)替代?
比如以上网址可以归纳为http://news.qq.com/newsgn/zhxw/shizhengxinwen_(*).htm

解决方案 »

  1.   

    算法需要建立在每组字符串都有某种相同的规律。
    如果是 array('hello', 'hellp', 'world'); 怎么表示? (*)(*)(*)(*)(*) ?
      

  2.   

    我已经写出来了。
    $url_arr = array('http://news.qq.com/newsgn/zhxw/shizhengxinwen.htm', 'http://news.qq.com/newsgn/zhxw/shizhengxinwen_3.htm', 'http://news.qq.com/newsgn/zhxw/shizhengxinwen_4.htm', 'http://news.qq.com/newsgn/zhxw/shizhengxinwen_5.htm');function get_url_diff($url_arr){
    foreach($url_arr as $v){
    preg_match_all("/[\d]+/", $v, $arr);
    $v_arr[] = $arr[0];
    }
    foreach($v_arr as $k => $v){
    if(!$v) {
    unset($url_arr[$k]);
    continue;
    }
    $split_arr[] = $v;
    }
    $t_arr = $split_arr;
    $split_rand_key = array_rand($split_arr);
    unset($t_arr[$split_rand_key]);
    $t_rand_key = array_rand($t_arr);
    $t_v = $t_arr[$t_rand_key];
    //print_r($t_v);
    foreach($split_arr[$split_rand_key] as $k => $v){
    if($v == $t_v[$k]) continue;
    $diff_key = $k;
    }
    $rand_key = array_rand($url_arr);
    $temp_url = $url_arr[$rand_key];
    $s_arr = preg_split("/[\d]+/", $temp_url);
    $split_arr[$split_rand_key][$diff_key] = '(*)';
    $url = '';
    foreach($s_arr as $k => $v){
    $url .= $v.$split_arr[$split_rand_key][$k];
    }
    return $url;
    }
    echo get_url_diff($url_arr);
      

  3.   


    可能我没有把前提描述清楚,我主要对一些url进行简单的分析就行了。如果是字符串,那就很复杂了。不讨论那些复杂的情况。