How should I deal with spaces in my dynamic (php) links ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeddiki
    Contributor
    • Jan 2009
    • 290

    How should I deal with spaces in my dynamic (php) links ?

    Hi,
    I am generating dynamic links and some don't work.

    Is it because they have a space in the link ?

    eg: from the name "David Franklyn", I get the link

    <a href="www.mysit e/David Franklyn/sports/Franklyn.html"> David Franklyn</a>

    What would be the best way to deal with the spaces ?

    Is it wrong to have spaces in a URL ?

    Should I replace with the underscore
    or the dash or period ?

    Thanks for your advice
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    If names with spaces in is the common denominator, then take a look at: http://uk2.php.net/urlencode

    Also, is that the actual output?
    Code:
    <a href="www.mysite/David Franklyn/sports/Franklyn.html">David Franklyn</a>
    If it is, you're missing the .com, .co.uk, etc.

    Comment

    • jeddiki
      Contributor
      • Jan 2009
      • 290

      #3
      Hi,
      I took the ".com" off so that this forum s/w didn't turn it nto a link.

      The urlencode() changes spaces to "%20"

      so I guess that is ok to use.
      Maybe its the best thing to use ?

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by jeddiki
        Hi,
        I took the ".com" off so that this forum s/w didn't turn it nto a link.

        The urlencode() changes spaces to "%20"

        so I guess that is ok to use.
        Maybe its the best thing to use ?
        Sure, %20 and + both do the same thing (near enough). Does encoding your url work then?

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Hi.

          One other thing, your URL is missing the "http://" or "https://" part.
          If you omit them the URL will be treated as a relative path and simply added to the current URL.

          That is, if you are currently on: "http://www.example.com "
          And you click a link that links to: "www.php.ne t"
          You end up at "http://www.example.com/www.php.net"

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by Atli
            Hi.

            One other thing, your URL is missing the "http://" or "https://" part.
            If you omit them the URL will be treated as a relative path and simply added to the current URL.

            That is, if you are currently on: "http://www.example.com "
            And you click a link that links to: "www.php.ne t"
            You end up at "http://www.example.com/www.php.net"
            You know, I thought that, but then for some reason I decided it was wrong. Darn!

            Comment

            • jeddiki
              Contributor
              • Jan 2009
              • 290

              #7
              Thanks for input.

              Actually these are all internal links, I added the "www.mysite/" just to make
              more obvious that it is a link.

              so in fact I am talking a link that looks like :
              David Franklyn/sports/Franklyn.html
              or
              David_Franklyn/sports/Franklyn.html
              or
              David-Franklyn/sports/Franklyn.html
              or
              David%20Frankly n/sports/Franklyn.html

              Comment

              • TheServant
                Recognized Expert Top Contributor
                • Feb 2008
                • 1168

                #8
                I don't know if you can name a folder or page with a space in it. So no, I think you need either no space or an underscore.

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  Originally posted by jeddiki
                  Actually these are all internal links, I added the "www.mysite/" just to make
                  more obvious that it is a link.
                  Ok. For future reference, please don't do that. It is easier for us to understand what is going on if we are looking at the actual code or data you are using.

                  Originally posted by TheServant
                  I don't know if you can name a folder or page with a space in it. So no, I think you need either no space or an underscore.
                  What OS do you use... Windows 95? :P

                  Unless you are using some ancient system, you can use spaces in folder names, but this can cause problems in some cases. It is best to avoid doing this.

                  If the path is going to be a part of an URI, don't use spaces. Use underscores, or one of the many naming conventions typically used for computer programming.
                  likeWhenWeCapit alizeEveryWord, or add_underscores _to_seperate_th em.

                  If you use actual spaces, it will have to be converted to %20 for the URL, which is not good.

                  Comment

                  • TheServant
                    Recognized Expert Top Contributor
                    • Feb 2008
                    • 1168

                    #10
                    Originally posted by Atli
                    What OS do you use... Windows 95? :P
                    lol. I was just taught not to use them and I guess I must have presumed that they eithe don't work or cause problems. But even for readability, I find using spaces seems to separate what is actually together. And to clarify, this is my web folders view, not windows folders :P

                    Comment

                    Working...