Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Wordpress sanitize filename函数从PHP转换为C#_C#_Asp.net_.net_Regex - Fatal编程技术网

将Wordpress sanitize filename函数从PHP转换为C#

将Wordpress sanitize filename函数从PHP转换为C#,c#,asp.net,.net,regex,C#,Asp.net,.net,Regex,我正在尝试将Wordpresssanitize_file_name函数从PHP转换为C,这样我就可以在自己构建的web应用程序上为我的站点文章生成unicode段塞 这是我的班级: using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Web; namespace MyProject.Helpers { pub

我正在尝试将Wordpress
sanitize_file_name
函数从PHP转换为C,这样我就可以在自己构建的web应用程序上为我的站点文章生成unicode段塞

这是我的班级:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

 namespace MyProject.Helpers
{
 public static class Slug
 {

    public static string SanitizeFileName(string filename)
    {

        string[] specialChars = { "?", "[", "]", "/", "\\", "=", "< ", "> ", ":", ";", ",", "'", "\"", "& ", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}" };

        filename = MyStrReplace(filename, specialChars, "");
        filename = Regex.Replace(filename, @"/[\s-]+/", "-");

        filename.TrimEnd('-').TrimStart('-');
        filename.TrimEnd('.').TrimStart('.');
        filename.TrimEnd('_').TrimStart('_');


        return filename;
    }

    private static string MyStrReplace(string strToCheck, string[] strToReplace, string newValue)
    {
        foreach (string s in strToReplace)
        {
            strToCheck = strToCheck.Replace(s, newValue);
        }
        return strToCheck;
    }

   // source: http://stackoverflow.com/questions/166855/c-sharp-preg-replace

    public static string PregReplace(string input, string[] pattern, string[] replacements)
    {
        if (replacements.Length != pattern.Length)
            throw new ArgumentException("Replacement and Pattern Arrays must be balanced");

        for (int i = 0; i < pattern.Length; i++)
        {
            input = Regex.Replace(input, pattern[i], replacements[i]);
        }

        return input;
    }
 }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text.RegularExpressions;
使用System.Web;
命名空间MyProject.Helpers
{
公共静态类Slug
{
公共静态字符串SanitizeFileName(字符串文件名)
{
字符串[]specialChars={“?”、“[”、“]”、“/”、“\\”、“=”、“<”、“>”、“:”、“;”、“、”、“、”、“\”、“&“、”、“$”、“*”、“(“、”、”、“、”、“、”、“、”、“、”、“!”、“{、”、“}}”;
filename=MyStrReplace(文件名,specialChars,“”);
filename=Regex.Replace(文件名@“/[\s-]+/”,“-”;
filename.TrimEnd('-').TrimStart('-');
filename.TrimEnd('.').TrimStart('.');
filename.TrimEnd(“”“).TrimStart(“”“);
返回文件名;
}
私有静态字符串MyStrReplace(字符串strToCheck,字符串[]strToReplace,字符串newValue)
{
foreach(strToReplace中的字符串s)
{
strToCheck=strToCheck.Replace(s,newValue);
}
返回strotcheck;
}
//资料来源:http://stackoverflow.com/questions/166855/c-sharp-preg-replace
公共静态字符串替换(字符串输入、字符串[]模式、字符串[]替换)
{
if(replacements.Length!=pattern.Length)
抛出新ArgumentException(“替换数组和模式数组必须平衡”);
for(int i=0;i
我写了一个标题,比如:
“假设我有——在那里做什么”
,但是我只得到了一个撇号修剪后的相同结果(let's->lets),没有其他改变

我需要与Wordpress.相同的等效转换。使用ASP.NET 4.5/C#

因为在C中没有动作修饰符,所以没有正则表达式分隔符

解决方案只是从模式中删除
/
符号:

filename = Regex.Replace(filename, @"[\s-]+", "-");
                                    ^      ^

不要在C#中使用正则表达式分隔符。从模式中删除
/
。@Stribizev是的,我删除了它,它似乎可以工作。我将再次测试它,以确保整个函数按预期工作。谢谢你。谢谢你的快速回答。它说了59秒,所以我去吃饭:)我不会忘记。再次感谢