我正在尝试将以下 slugify 方法从 PHP 转换为 C#: http ://snipplr.com/view/22741/slugify-a-string-in-php/
编辑:为方便起见,这里是上面的代码:
/**
* Modifies a string to remove al non ASCII characters and spaces.
*/
static public function slugify($text)
{
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
// trim
$text = trim($text, '-');
// transliterate
if (function_exists('iconv'))
{
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
}
// lowercase
$text = strtolower($text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
if (empty($text))
{
return 'n-a';
}
return $text;
}
除了找不到与以下 PHP 代码行等效的 C# 代码之外,其余部分的代码我没有遇到任何问题:
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
编辑:
这样做的目的是将非ASCII字符翻译Reformáció Genfi Emlékműve Előtt
成reformacio-genfi-emlekmuve-elott