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 6136997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:40:22+00:00 2026-05-23T17:40:22+00:00

Im trying to port this function to c# http://www.phpsnaps.com/snaps/view/clean-url/ I have problems to transform

  • 0

Im trying to port this function to c#

http://www.phpsnaps.com/snaps/view/clean-url/

I have problems to transform “”~[^-a-z0-9_]+~” (php pattern nomenclature) in regex charp nomenclature.

<?php

function cleanURL($string)
{
    $url = str_replace("'", '', $string);
    $url = str_replace('%20', ' ', $url);
    // (PROBLEM) substitutes anything but letters, numbers and '_' with separator
    $url = preg_replace('~[^\pL0-9_]+~u', '-', $url);
    $url = trim($url, "-");
    // you may opt for your own custom character map for encoding.
    $url = iconv("utf-8", "us-ascii//TRANSLIT", $url); 
    $url = strtolower($url);
     (PROBLEM)
    $url = preg_replace('~[^-a-z0-9_]+~', '', $url); // keep only letters, numbers, '_' and separator
    return $url;
} // echo cleanURL("Shelly's%20Greatest%20Poem%20(2008)");  // shellys-greatest-poem-2008
?>

This is c# function:

static String cleanURL(String url)
{
    url = url.Replace("'", "");
    url = url.Replace("%20", " ");            
    url = System.Text.RegularExpressions.Regex.Replace(url, "~[^\pL0-9_]+~u", "-");           
    url = url.Trim(new char[1]{'-'});         

    Encoding ascii = Encoding.ASCII;           
    Encoding utf8 = Encoding.UTF8;           
    byte[] utf8bytes = utf8.GetBytes(url);           
    byte[] asciiBytes = Encoding.Convert(utf8, ascii, utf8bytes);            
    char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];           
    ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);           

    url = new string(asciiChars);           
    url = url.ToLower();                    
    url = System.Text.RegularExpressions.Regex.Replace(url, "~[^-a-z0-9_]+~", "");
    return url;           
}    

Thanks. Any can help me?

  • 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-05-23T17:40:22+00:00Added an answer on May 23, 2026 at 5:40 pm

    The ~ at start and end are just pattern start end markers, they are not needed in the c# format

    so ~[^-a-z0-9_]~ should just be [^-a-z0-9_]

    The u on the end of the first pattern makes php treat the pattern as UTF8, you shouldn’t need this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement something like this: <div> <table> <thead> <tr> <td>Port name</td> <td>Current
I am trying to make a proxy server that gets a page from www.xxx.com
Have been struggling all day trying to make this simple example work using socket.io.
I have the following code (I'm working from code at http://www.linuxhowtos.org/C_C++/socket.htm ) which I'm
Trying to find out what the (float) in this function means: static float avgEndpoints
I have this hook function that works fine on x86. Thanks to Bo Persson.
I'm currently trying to port a SIP stack library (pjSIP) to the PSP Console
I'm trying to port an application to Mono, however Mono doesn't support Application Settings.
i'm trying to port some Java stuff to C#. I'm just wondering if the
I am trying to configure the website SSL port with the certificate for a

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.