douke1905 2012-08-23 17:57
浏览 14
已采纳

php str_word_count到c#的端口

I'm migrating a legacy PHP application to .net, and one of the requirements is that the URLs stay exactly as before.

To generate friendly URLs the legacy application uses str_word_count, I was wondering if there is a port of this function to C#?

  • 写回答

1条回答 默认 最新

  • duanjue2576 2012-08-23 18:41
    关注

    Okay here's my "bad C#" example (mimicking PHP in the mixed return type). It's a fairly trivial implementation leveraging .NET's Regular Expressions.

    private enum WORD_FORMAT
    {
        NUMBER = 0,
        ARRAY = 1,
        ASSOC = 2
    };
    
    private static object str_word_count(string str, WORD_FORMAT format, string charlist)
    {
        string wordchars = string.Format("{0}{1}", "a-z", Regex.Escape(charlist));
    
        var words = Regex.Matches(str, string.Format("[{0}]+(?:[{0}'\\-]+[{0}])?", wordchars), RegexOptions.Compiled | RegexOptions.IgnoreCase);
    
        if (format == WORD_FORMAT.ASSOC)
        {
            var assoc = new Dictionary<int, string>(words.Count);
            foreach (Match m in words)
                assoc.Add(m.Index, m.Value);
            return assoc;
        }
        else if (format == WORD_FORMAT.ARRAY)
        {
            return words.Cast<Match>().Select(m => m.Value).ToArray();
        }
        else // default to number.
        {
            return words.Count;
        }
    }
    

    So the function will return a Dictionary<int,string> if you choose ASSOC, a string[] if you choose ARRAY and a simple int if you choose NUMBER.

    An example (I copied PHP's example here

    static void Main(string[] args)
    {
        string sentence = @"Hello fri3nd, you're
       looking          good today!";
    
        var assoc = (Dictionary<int,string>)str_word_count(sentence, WORD_FORMAT.ASSOC, string.Empty);
        var array = (string[])str_word_count(sentence, WORD_FORMAT.ARRAY, string.Empty);
        var number = (int)str_word_count(sentence, WORD_FORMAT.NUMBER, string.Empty);
    
        //test the plain array
        Console.WriteLine("Array
    (");
        for (int i = 0; i < array.Length; i++)
            Console.WriteLine("\t[{0}] => {1}", i, array[i]);
        Console.WriteLine(")");
        // test the associative
        Console.WriteLine("Array
    (");
        foreach (var kvp in assoc)
            Console.WriteLine("\t[{0}] => {1}", kvp.Key, kvp.Value);
        Console.WriteLine(")");
        //test the charlist:
        array = (string[])str_word_count(sentence, WORD_FORMAT.ARRAY, "àáãç3");
        Console.WriteLine("Array
    (");
        for (int i = 0; i < array.Length; i++)
            Console.WriteLine("\t[{0}] => {1}", i, array[i]);
        Console.WriteLine(")");
        //test the number
        Console.WriteLine("
    {0}", number);
        Console.Read();
    }
    

    But, I'd like to add a note here: Don't return objects. It works aok with PHP because it's not a strongly typed language. Really, you should be writing individual versions of the function to cater for each different format. Anyways, that should get you started :)

    Output:

    Array
    (
        [0] => Hello
        [1] => fri
        [2] => nd
        [3] => you're
        [4] => looking
        [5] => good
        [6] => today
    )
    Array
    (
        [0] => Hello
        [6] => fri
        [10] => nd
        [14] => you're
        [25] => looking
        [42] => good
        [47] => today
    )
    Array
    (
        [0] => Hello
        [1] => fri3nd
        [2] => you're
        [3] => looking
        [4] => good
        [5] => today
    )
    
    7
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路