This is a discussion on String manipulation. within the PHP Language forums, part of the PHP Programming Forums category; Hi, I've got a php script that receives parts of URLs from another script and transforms it to be ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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%20directory/". 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 |
|
|||
|
Nuno Paquete wrote:
> 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%20directory/". > 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"); You're killing flies with a shotgun. What you really need is http://www.php.net/manual/en/function.urlencode.php Berislav -- If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are Groucho, Chico, and Harpo, then Usenet is Zeppo. |
|
|||
|
*** Nuno Paquete wrote/escribió (Fri, 20 Aug 2004 11:53:01 +0100):
> For example, if I receive a parameter like "dir=some directory/" I have to > convert it to "dir=some%20directory/". Two options: echo urlencode('dir=some directory/'); // prints 'dir=some+directory/' echo rawurlencode('dir=some directory/'); // prints 'dir=some%20directory/' 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 ;-) -- |
|
|||
|
Alvaro G Vicario wrote:
[ ... ] > echo urlencode('dir=some directory/'); // prints 'dir=some+directory/' > echo rawurlencode('dir=some directory/'); // prints 'dir=some%20directory/' But remember that equals signs and solidi are percent- encoded too. Your examples output 'dir%3Dsome+directory%2F' and 'dir%3Dsome%20directory%2F' respectively. > I'm unsure though about what standards say about encoding spaces in URLs > (i.e, which is better, "+" or "%20"?) 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 |
|
|||
|
Alvaro G Vicario wrote:
> *** Nuno Paquete wrote/escribió (Fri, 20 Aug 2004 11:53:01 +0100): >> For example, if I receive a parameter like "dir=some directory/" I have >> to convert it to "dir=some%20directory/". > > Two options: > > echo urlencode('dir=some directory/'); // prints 'dir=some+directory/' > echo rawurlencode('dir=some directory/'); // prints > 'dir=some%20directory/' > > I'm unsure though about what standards say about encoding spaces in URLs > (i.e, which is better, "+" or "%20"?) > 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. |
|
|||
|
Nuno Paquete wrote:
[ ... ] > 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? 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%20directory', the percent sign must itself be percent-encoded as %25, giving a query component of 'dir=some%2520directory'. > So, I need to encode it again (understandable), but I don't want to enconde > '/' characters. > > Any suggestions? How did you do it before? -- Jock |
|
|||
|
> 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%20directory" becomes "some directory". Any suggestions? Thanks again. |
|
|||
|
Nuno Paquete wrote:
>> 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%20directory" becomes "some directory". > Any suggestions? > > Thanks again. 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 |
|
|||
|
*** Nuno Paquete wrote/escribió (Sat, 21 Aug 2004 14:18:45 +0100):
> The functions you described both encode everything, including slashs (/), > changing it for %2F. I want to keep '/' unchanged. 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 ;-) -- |
|
|||
|
*** Alvaro G Vicario wrote/escribió (Mon, 23 Aug 2004 08:19:59 +0200):
> Also, as John said, it also encodes the equal size sign, not size. Sorry for the typos... -- -- Álvaro G. Vicario - Burgos, Spain -- Questions sent to my mailbox will be billed ;-) -- |