我从这个问题中得到了以下代码:What is the best way to clean a string for placement in a URL, like the question name on SO?
$str = strtolower(
preg_replace( array('/[^a-z0-9\- ]/i', '/[ \-]+/'), array('', '-'),
$urlPart ) );
此代码适用于我的 PHP,它仅在一行中制作干净和 SEO 的 url,但我想在我的 ASP.NET(C#) 应用程序中使用相同的脚本。但是不知道 C# 中这一行的代码是什么。任何人都可以将此 PHP 代码转换为 C# 函数。
谢谢
最佳答案
为简单起见,使用两个正则表达式替换调用可能最简单,尽管您可以使用Replace
重载和 MatchEvaluator 将其重写为一个
(这会有点 hacky)。
using System.Text.RegularExpressions;
// ...
var str = Regex.Replace(Regex.Replace(urlPart, @"[^a-z0-9\- ]/i", ""), @"[ \-]+", "-").ToLower();
希望对您有所帮助。
关于c# - 如何将此 PHP 脚本转换为 C#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/812083/