dsjklb0205 2011-07-12 15:38
浏览 37
已采纳

帮助端口php函数做“清理网址”到c#

Im trying to port this function to c#

http://www.phpsnaps.com/snaps/view/clean-url/

I have problems to transform ""~[^-a-z0-9_]+~" (php pattern nomenclature) in regex charp nomenclature.

<?php

function cleanURL($string)
{
    $url = str_replace("'", '', $string);
    $url = str_replace('%20', ' ', $url);
    // (PROBLEM) substitutes anything but letters, numbers and '_' with separator
    $url = preg_replace('~[^\pL0-9_]+~u', '-', $url);
    $url = trim($url, "-");
    // you may opt for your own custom character map for encoding.
    $url = iconv("utf-8", "us-ascii//TRANSLIT", $url); 
    $url = strtolower($url);
     (PROBLEM)
    $url = preg_replace('~[^-a-z0-9_]+~', '', $url); // keep only letters, numbers, '_' and separator
    return $url;
} // echo cleanURL("Shelly's%20Greatest%20Poem%20(2008)");  // shellys-greatest-poem-2008
?>

This is c# function:

static String cleanURL(String url)
{
    url = url.Replace("'", "");
    url = url.Replace("%20", " ");            
    url = System.Text.RegularExpressions.Regex.Replace(url, "~[^\pL0-9_]+~u", "-");           
    url = url.Trim(new char[1]{'-'});         

    Encoding ascii = Encoding.ASCII;           
    Encoding utf8 = Encoding.UTF8;           
    byte[] utf8bytes = utf8.GetBytes(url);           
    byte[] asciiBytes = Encoding.Convert(utf8, ascii, utf8bytes);            
    char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];           
    ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);           

    url = new string(asciiChars);           
    url = url.ToLower();                    
    url = System.Text.RegularExpressions.Regex.Replace(url, "~[^-a-z0-9_]+~", "");
    return url;           
}    

Thanks. Any can help me?

  • 写回答

1条回答 默认 最新

  • dongyuchen9276 2011-07-12 15:42
    关注

    The ~ at start and end are just pattern start end markers, they are not needed in the c# format

    so ~[^-a-z0-9_]~ should just be [^-a-z0-9_]

    The u on the end of the first pattern makes php treat the pattern as UTF8, you shouldn't need this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程