2009-01-21 13 views
17

取一個字符串,如:如何從字符串創建一個SEO友好的以短劃線分隔的URL?

在C#中:如何以逗號分隔的字符串列表中添加「行情」,圍繞字符串?

並將其轉換爲:

在-C-怎麼辦,我加引號圍繞頭字符串中 - - 逗號分隔的列表的串一個

要求:

  • 分隔每個由短劃線字,並刪除所有標點符號
  • (考慮到不是所有的字被用空格分開。)
  • 函數佔用最大長度,並獲取低於該最大長度的所有令牌。例如:ToSeoFriendly("hello world hello world", 14)返回"hello-world"
  • 所有單詞都轉換爲小寫。

在另一張紙條上,是否應該有最小長度?

+1

這裏的一對URL一些信息長度:http://www.boutell。com/newfaq/misc/urllength.html – bchhun 2009-01-21 16:38:51

+1

也許用他們的「英語發音」替換一些特殊字符,例如「#」=>「尖銳」,將允許製作更好的網址,並將C與C#區分開來(這很好,對吧?)?)? – Wookai 2009-01-21 17:17:05

+0

是肯定的,除非#是不尖銳,這是一個不同的符號; p – Shawn 2009-01-22 03:53:09

回答

10

這裏是我的在C#解決方案#

private string ToSeoFriendly(string title, int maxLength) { 
    var match = Regex.Match(title.ToLower(), "[\\w]+"); 
    StringBuilder result = new StringBuilder(""); 
    bool maxLengthHit = false; 
    while (match.Success && !maxLengthHit) { 
     if (result.Length + match.Value.Length <= maxLength) { 
      result.Append(match.Value + "-"); 
     } else { 
      maxLengthHit = true; 
      // Handle a situation where there is only one word and it is greater than the max length. 
      if (result.Length == 0) result.Append(match.Value.Substring(0, maxLength)); 
     } 
     match = match.NextMatch(); 
    } 
    // Remove trailing '-' 
    if (result[result.Length - 1] == '-') result.Remove(result.Length - 1, 1); 
    return result.ToString(); 
} 
2

下面是PHP的一個解決方案:

function make_uri($input, $max_length) { 
    if (function_exists('iconv')) { 
    $input = @iconv('UTF-8', 'ASCII//TRANSLIT', $input); 
    } 

    $lower = strtolower($input); 


    $without_special = preg_replace_all('/[^a-z0-9 ]/', '', $input); 
    $tokens = preg_split('/ +/', $without_special); 

    $result = ''; 

    for ($tokens as $token) { 
    if (strlen($result.'-'.$token) > $max_length+1) { 
     break; 
    } 

    $result .= '-'.$token;  
    } 

    return substr($result, 1); 
} 

用法:

echo make_uri('In C#: How do I add "Quotes" around string in a ...', 500); 

除非你需要的URI是鍵式,他們並不需要小。但是你應該指定一個最大值,以便這些網址可以很好地與代理服務器一起工作

7

我會請按照下列步驟操作:

  1. 字符串轉換爲小寫
  2. 用連字符替換不需要的字符
  3. 由一個連字符替換多個連字符(不是必要的,因爲preg_replace()函數調用已防止多個連字符)
  4. 如果需要,在開始和結束時刪除超鏈接
  5. 如果從最後連字符所需位置之前X到年底

所以,都聚集在一個函數(PHP)

  • 裝飾:

    function generateUrlSlug($string, $maxlen=0) 
    { 
        $string = trim(preg_replace('/[^a-z0-9]+/', '-', strtolower($string)), '-'); 
        if ($maxlen && strlen($string) > $maxlen) { 
         $string = substr($string, 0, $maxlen); 
         $pos = strrpos($string, '-'); 
         if ($pos > 0) { 
          $string = substr($string, 0, $pos); 
         } 
        } 
        return $string; 
    } 
    
  • 4

    C#

    public string toFriendly(string subject) 
    { 
        subject = subject.Trim().ToLower(); 
        subject = Regex.Replace(subject, @"\s+", "-"); 
        subject = Regex.Replace(subject, @"[^A-Za-z0-9_-]", ""); 
        return subject; 
    } 
    
    0

    一個稍微乾淨的方式做t他在PHP至少是:

    function CleanForUrl($urlPart, $maxLength = null) { 
        $url = strtolower(preg_replace(array('/[^a-z0-9\- ]/i', '/[ \-]+/'), array('', '-'), trim($urlPart))); 
        if ($maxLength) $url = substr($url, 0, $maxLength); 
        return $url; 
    } 
    

    還不如做trim()在開始所以以後少來處理和完全替代在用preg_replace()完成。

    Thxs爲想出這個最CG:What is the best way to clean a string for placement in a URL, like the question name on SO?

    2

    一個更好的版本:

    function Slugify($string) 
    { 
        return strtolower(trim(preg_replace(array('~[^0-9a-z]~i', '~-+~'), '-', $string), '-')); 
    } 
    
    0

    在動態URL,這些ID是通過查詢字符串的腳本,通過..因爲大多數搜索引擎將短劃線視爲... NET:SEO開發人員指南還涵蓋了這三種附加方法 search engine optimization

    1

    Perl中的解決方案:

    my $input = 'In C#: How do I add "Quotes" around string in a comma delimited list of strings?'; 
    
    my $length = 20; 
    $input =~ s/[^a-z0-9]+/-/gi; 
    $input =~ s/^(.{1,$length}).*/\L$1/; 
    
    print "$input\n"; 
    

    完成。

    1

    解決方案在外殼:

    echo 'In C#: How do I add "Quotes" around string in a comma delimited list of strings?' | \ 
        tr A-Z a-z | \ 
        sed 's/[^a-z0-9]\+/-/g;s/^\(.\{1,20\}\).*/\1/' 
    
    0

    另一個季節,另一個原因,選擇紅寶石 :)

    def seo_friendly(str) 
        str.strip.downcase.gsub /\W+/, '-' 
    end 
    

    這就是全部。

    0

    在Python中,(如果已安裝的Django,即使你使用的是其他框架。)

    from django.template.defaultfilters import slugify 
    slugify("In C#: How do I add "Quotes" around string in a comma delimited list of strings?") 
    
    1

    這是接近堆棧溢出是如何產生蛞蝓:

    public static string GenerateSlug(string title) 
    { 
        string slug = title.ToLower(); 
        if (slug.Length > 81) 
         slug = slug.Substring(0, 81); 
        slug = Regex.Replace(slug, @"[^a-z0-9\-_\./\\ ]+", ""); 
        slug = Regex.Replace(slug, @"[^a-z0-9]+", "-"); 
    
        if (slug[slug.Length - 1] == '-') 
         slug = slug.Remove(slug.Length - 1, 1); 
        return slug; 
    }