Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Url Encoding

Options
  • 18-02-2004 3:02pm
    #1
    Closed Accounts Posts: 30


    how should a space be encoded in url encoding?
    sometimes is seems like it should be a '+', such as when posting data to cgi.
    just type in this google query, "two words", and look at the url.

    however, sometimes it seems it should be a %20, as in web addresses that have spaces in them.

    in c#, HttpUtility.UrlEncode(string) converts spaces to +'s.
    this is fine in most cases but in one particular case i would like it to convert to %20.

    is there another method for this? apparently javascript's urlencode works like this.
    anyway, if not, i suppose i could use one of these reqular expression things to substitute + with %20.

    somebody want to save me the trouble of learning the most cryptic language since hieroglyphics and post the regex (using c# if possible)?

    cheers.


Comments

  • Registered Users Posts: 629 ✭✭✭str8_away


    It should be %20.
    "+" in google means - search for word1 plus word2

    % means the following 2 charactors are Hex number
    20 Hex number for Space on ASCII table

    example:
    %21 = !
    %30 = 0
    %41 = A
    %61 = a


    You can try by replace all letters in google search URL to %xx and you will get the same search result.


    Sorry don't know anything about C# so can't help you there:(


  • Closed Accounts Posts: 30 Lu[ifer


    i don't think its that simple.

    check out http://www.simplelogic.com/Developer/InetEncode.asp

    url encoding seems to encode spaces as +'s.

    only when the space is part of an actual url address, is the %20 notation used.

    i'm still not 100% pure as to if, when, where, why and how, but as i already said, i don't think its that simple.


  • Registered Users Posts: 629 ✭✭✭str8_away


    From your link

    URL PATH: The path to your web page

    URL: This should be called URL parameter. Parameter you passed in for your page. It depend on your code you can use %20 or +. but search engins use "+" to replace space.

    Just make a simple page and you will see how it works :D


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    A url embedded within a web page (i.e. in an <a> tag) should use the normal identifiers for special characters. That is, if there are spaces and ampersands in the url, there should be &nbsp; and &amp;, respectively.


  • Closed Accounts Posts: 30 Lu[ifer


    well after having done some research into this, i found out the following...

    php, provides two functions, urlencode() and rawurlencode()
    the former encodes space as + and the latter as %20

    javascript provides escape() and encodeURI()
    both encode space as %20.

    perl provides uri_escape() which takes a second optional argument which allows you specify which characters are to be consisdered unsafe.

    anyway, in c# there is only the UrlEnocde() function which only encodes space to + which was not working for me so after learning all about regular expressions and the RegEx class, i came up with the following...

    [php]
    using System.Text.RegularExpressions;

    string str = "some spaces and <>£$^& crazy charaters";

    string enc = Regex.Replace(message, "([^A-Za-z0-9])", new MatchEvaluator(UrlEncode));


    static string UrlEncode(Match m)
    {
    return String.Format("%{0:x2}", (int)m.Value[0]);
    }

    [/php]


  • Advertisement
Advertisement