String manipulation.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nuno Paquete

    String manipulation.

    Hi,

    I've got a php script that receives parts of URLs from another script and
    transforms it to be HTTP compliant.
    For example, if I receive a parameter like "dir=some directory/" I have to
    convert it to "dir=some%20dir ectory/".
    I make this with this 2 lines of code:

    $str = explode(" ",$_GET["dir"]);
    fputs($fp,"GET /share/" . implode("%20",$ str) . " HTTP/1.0\r\n");

    Everything was working fine, until I have created a directory called "C++".
    I don't know why but the code above convert it to "C ".
    It should keep it unchanged, because what the code do is searching for SPACE
    characters ' ' only and change it to "%20". I was not expecting that it
    would change '+' characters.
    What is wrong with the code?

    Thanks in advance,
    Nuno Paquete


  • Berislav Lopac

    #2
    Re: String manipulation.

    Nuno Paquete wrote:[color=blue]
    > Hi,
    >
    > I've got a php script that receives parts of URLs from another script
    > and transforms it to be HTTP compliant.
    > For example, if I receive a parameter like "dir=some directory/" I
    > have to convert it to "dir=some%20dir ectory/".
    > I make this with this 2 lines of code:
    >
    > $str = explode(" ",$_GET["dir"]);
    > fputs($fp,"GET /share/" . implode("%20",$ str) . " HTTP/1.0\r\n");[/color]

    You're killing flies with a shotgun. What you really need is
    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    Berislav

    --
    If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
    Groucho, Chico, and Harpo, then Usenet is Zeppo.


    Comment

    • Alvaro G Vicario

      #3
      Re: String manipulation.

      *** Nuno Paquete wrote/escribió (Fri, 20 Aug 2004 11:53:01 +0100):[color=blue]
      > For example, if I receive a parameter like "dir=some directory/" I have to
      > convert it to "dir=some%20dir ectory/".[/color]

      Two options:

      echo urlencode('dir= some directory/'); // prints 'dir=some+direc tory/'
      echo rawurlencode('d ir=some directory/'); // prints 'dir=some%20dir ectory/'

      I'm unsure though about what standards say about encoding spaces in URLs
      (i.e, which is better, "+" or "%20"?)

      --
      -- Álvaro G. Vicario - Burgos, Spain
      -- Questions sent to my mailbox will be billed ;-)
      --

      Comment

      • John Dunlop

        #4
        Re: String manipulation.

        Alvaro G Vicario wrote:

        [ ... ]
        [color=blue]
        > echo urlencode('dir= some directory/'); // prints 'dir=some+direc tory/'
        > echo rawurlencode('d ir=some directory/'); // prints 'dir=some%20dir ectory/'[/color]

        But remember that equals signs and solidi are percent-
        encoded too. Your examples output 'dir%3Dsome+dir ectory%2F'
        and 'dir%3Dsome%20d irectory%2F' respectively.
        [color=blue]
        > I'm unsure though about what standards say about encoding spaces in URLs
        > (i.e, which is better, "+" or "%20"?)[/color]

        I had misunderstood this, but it was explained to me a while
        ago. Hopefully now I have it right. ;o)

        The plus sign is reserved in query components, which means
        the semantics of the URI change if it's replaced by its
        percent-encoding. How it's interpreted is up to the program
        handling it. The URI standard doesn't assign any meaning to
        plus signs; whereas %20 always means a US-ASCII space.

        HAGW Álvaro!

        --
        Jock

        Comment

        • Nuno Paquete

          #5
          Re: String manipulation.

          Alvaro G Vicario wrote:
          [color=blue]
          > *** Nuno Paquete wrote/escribió (Fri, 20 Aug 2004 11:53:01 +0100):[color=green]
          >> For example, if I receive a parameter like "dir=some directory/" I have
          >> to convert it to "dir=some%20dir ectory/".[/color]
          >
          > Two options:
          >
          > echo urlencode('dir= some directory/'); // prints 'dir=some+direc tory/'
          > echo rawurlencode('d ir=some directory/'); // prints
          > 'dir=some%20dir ectory/'
          >
          > I'm unsure though about what standards say about encoding spaces in URLs
          > (i.e, which is better, "+" or "%20"?)
          >[/color]

          Hi.
          The functions you described both encode everything, including slashs (/),
          changing it for %2F. I want to keep '/' unchanged.
          What I think that is strange is that I want to manipulate a string that was
          supposed to be already encoded, because when the script is asked from
          another page, it is accessed from a link like this: "script.php?dir =some
          20directory" but then, inside script.php $_GET["dir"] becomes "some
          directory". Why?
          So, I need to encode it again (understandable ), but I don't want to enconde
          '/' characters.

          Any suggestions?

          Regards.

          Comment

          • John Dunlop

            #6
            Re: String manipulation.

            Nuno Paquete wrote:

            [ ... ]
            [color=blue]
            > What I think that is strange is that I want to manipulate a string that was
            > supposed to be already encoded, because when the script is asked from
            > another page, it is accessed from a link like this: "script.php?dir =some
            > 20directory" but then, inside script.php $_GET["dir"] becomes "some
            > directory". Why?[/color]

            Because %20 means a space; %20 is the percent-encoding of a
            US-ASCII space character; see RFC2396 sec. 2.4. If you
            intended $_GET['dir'] to be 'some%20directo ry', the percent
            sign must itself be percent-encoded as %25, giving a query
            component of 'dir=some%2520d irectory'.
            [color=blue]
            > So, I need to encode it again (understandable ), but I don't want to enconde
            > '/' characters.
            >
            > Any suggestions?[/color]

            How did you do it before?

            --
            Jock

            Comment

            • Nuno Paquete

              #7
              Re: String manipulation.

              > How did you do it before?

              I get the result of the web server. This is a script that deals with HTTP
              connections. I establish the connection, I "speak" HTTP with the server
              (GET /dir/ HTTP/1.0) and receive the directory content (Index Directory).
              Then I manipulate the result, and this result already contains the url
              encoded.
              But now I need to POST the (already encoded) url and strangely I cant get
              the encoded result on the other side. It seems that it is decoded in the
              middle, because "some%20directo ry" becomes "some directory".
              Any suggestions?

              Thanks again.

              Comment

              • Nuno Paquete

                #8
                Re: String manipulation.

                Nuno Paquete wrote:
                [color=blue][color=green]
                >> How did you do it before?[/color]
                >
                > I get the result of the web server. This is a script that deals with HTTP
                > connections. I establish the connection, I "speak" HTTP with the server
                > (GET /dir/ HTTP/1.0) and receive the directory content (Index Directory).
                > Then I manipulate the result, and this result already contains the url
                > encoded.
                > But now I need to POST the (already encoded) url and strangely I cant get
                > the encoded result on the other side. It seems that it is decoded in the
                > middle, because "some%20directo ry" becomes "some directory".
                > Any suggestions?
                >
                > Thanks again.[/color]

                It's done.
                Instead of make links with GET method, I did make a form and then used POST
                method.
                It's working perfectly, and I don't need to use urlencode() function.

                Best regards,
                Nuno Paquete

                Comment

                • Alvaro G Vicario

                  #9
                  Re: String manipulation.

                  *** Nuno Paquete wrote/escribió (Sat, 21 Aug 2004 14:18:45 +0100):[color=blue]
                  > The functions you described both encode everything, including slashs (/),
                  > changing it for %2F. I want to keep '/' unchanged.[/color]

                  If the slash is part of the parameter you must encode it.

                  Also, as John said, it also encodes the equal size, which should be left
                  outside the function call.



                  --
                  -- Álvaro G. Vicario - Burgos, Spain
                  -- Questions sent to my mailbox will be billed ;-)
                  --

                  Comment

                  • Alvaro G Vicario

                    #10
                    Re: String manipulation.

                    *** Alvaro G Vicario wrote/escribió (Mon, 23 Aug 2004 08:19:59 +0200):[color=blue]
                    > Also, as John said, it also encodes the equal size[/color]

                    sign, not size. Sorry for the typos...


                    --
                    -- Álvaro G. Vicario - Burgos, Spain
                    -- Questions sent to my mailbox will be billed ;-)
                    --

                    Comment

                    Working...