我从这个问题中得到了以下代码: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的网址在一行,但我想使用相同的脚本在我的asp.net(c)应用程序。但不知道这一行的代码是什么。请把这个php代码转换成c函数。
谢谢

最佳答案

为了保持简单,使用两个regex replace调用可能是最简单的,不过您可以使用Replace重载和MatchEvaluator将其重写为一个regex replace调用(这可能有点麻烦)。

using System.Text.RegularExpressions;

// ...

var str = Regex.Replace(Regex.Replace(urlPart, @"[^a-z0-9\- ]/i", ""), @"[ \-]+", "-").ToLower();

希望能有所帮助。

07-24 09:38
查看更多