Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8426385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:31:23+00:00 2026-06-10T04:31:23+00:00

I’m migrating a legacy PHP application to .net, and one of the requirements is

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:31:24+00:00Added an answer on June 10, 2026 at 4:31 am

    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\n(");
        for (int i = 0; i < array.Length; i++)
            Console.WriteLine("\t[{0}] => {1}", i, array[i]);
        Console.WriteLine(")");
        // test the associative
        Console.WriteLine("Array\n(");
        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\n(");
        for (int i = 0; i < array.Length; i++)
            Console.WriteLine("\t[{0}] => {1}", i, array[i]);
        Console.WriteLine(")");
        //test the number
        Console.WriteLine("\n{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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.