Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Url Encoding

  • 18-02-2004 02: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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 68,173 ✭✭✭✭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