PHP inserting carriage returns into POST values?

PHP inserting carriage returns into POST values?

am 04.09.2009 21:16:47 von James Colannino

Hey everyone. I ran into a really weird issue that I was hoping I could
find some clarification on. In short, I have javascript functions that
operate on hidden text values. Those values may be posted, in which
case PHP then prints them back to the page via what comes in on $_POST.

The weird thing is, I was no longer able to match substrings inside
those hidden text values after posting via Javascript. I banged my head
over this for a couple hours, until I realized that the string's length
was being increased by one after posting (I found this out in
javascript). Upon further investigation, I found that all instances of
"substring\n" were being replaced by "substring(carriage return)\n"
after post.

For now, I'm simply doing str_replace(chr(13), "", $_POST['value'])
before re-inserting it into the HTML, but I was wondering why PHP is
inserting those extra characters.

Thanks!
James

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: PHP inserting carriage returns into POST values?

am 04.09.2009 21:31:08 von Andrew Ballard

On Fri, Sep 4, 2009 at 3:16 PM, James Colannino wrote:
> Hey everyone.  I ran into a really weird issue that I was hoping I c=
ould
> find some clarification on.  In short, I have javascript functions t=
hat
> operate on hidden text values.  Those values may be posted, in which
> case PHP then prints them back to the page via what comes in on $_POST.
>
> The weird thing is, I was no longer able to match substrings inside
> those hidden text values after posting via Javascript.  I banged my =
head
> over this for a couple hours, until I realized that the string's length
> was being increased by one after posting (I found this out in
> javascript).  Upon further investigation, I found that all instances=
of
> "substring\n" were being replaced by "substring(carriage return)\n"
> after post.
>
> For now, I'm simply doing str_replace(chr(13), "", $_POST['value'])
> before re-inserting it into the HTML, but I was wondering why PHP is
> inserting those extra characters.
>
> Thanks!
> James
>

Are you sure it is PHP? Just a couple ideas...

Javascript interpolates \n as a newline character inside both double
and single quotes unlike PHP that only interpolates inside double
quotes. If the client browser is running under Windows, it may even be
possible that the \n is recognized by the browser as a line terminator
and converted to the Windows line terminator sequence (\r\n) either
inside the form element itself or in the Javascript you are using to
post back to the web server.

Andrew

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: PHP inserting carriage returns into POST values?

am 04.09.2009 22:12:13 von James Colannino

Andrew Ballard wrote:

> Javascript interpolates \n as a newline character inside both double
> and single quotes unlike PHP that only interpolates inside double
> quotes. If the client browser is running under Windows, it may even be
> possible that the \n is recognized by the browser as a line terminator
> and converted to the Windows line terminator sequence (\r\n) either
> inside the form element itself or in the Javascript you are using to
> post back to the web server.

Both the server and the client are Linux machines, so there are no
issues with incompatible representations of newline. I only ever place
\n's inside double quotes in PHP, and am aware of the fact that I don't
have to do that in PHP. For the life of me, I just can't figure out
what's happening.

Anyway, for now, filtering \r's out in PHP seems to do the trick.

James

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: PHP inserting carriage returns into POST values?

am 05.09.2009 00:02:57 von news.NOSPAM.0ixbtqKe

On Fri, 04 Sep 2009 12:16:47 -0700, James Colannino wrote:

> Hey everyone. I ran into a really weird issue that I was hoping I could
> find some clarification on. In short, I have javascript functions that
> operate on hidden text values. Those values may be posted, in which
> case PHP then prints them back to the page via what comes in on $_POST.
>
> The weird thing is, I was no longer able to match substrings inside
> those hidden text values after posting via Javascript. I banged my head
> over this for a couple hours, until I realized that the string's length
> was being increased by one after posting (I found this out in
> javascript). Upon further investigation, I found that all instances of
> "substring\n" were being replaced by "substring(carriage return)\n"
> after post.

It may be the browser that is converting those line breaks.
From the HTML spec.:


:

application/x-www-form-urlencoded
...
Line breaks are represented as "CR LF" pairs (i.e., `%0D%0A').


:

multipart/form-data
...
"CR LF" (i.e., `%0D%0A') is used to separate lines of data.


/Nisse

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: PHP inserting carriage returns into POST values?

am 05.09.2009 00:05:13 von Paul M Foster

On Fri, Sep 04, 2009 at 12:16:47PM -0700, James Colannino wrote:

> Hey everyone. I ran into a really weird issue that I was hoping I could
> find some clarification on. In short, I have javascript functions that
> operate on hidden text values. Those values may be posted, in which
> case PHP then prints them back to the page via what comes in on $_POST.
>
> The weird thing is, I was no longer able to match substrings inside
> those hidden text values after posting via Javascript. I banged my head
> over this for a couple hours, until I realized that the string's length
> was being increased by one after posting (I found this out in
> javascript). Upon further investigation, I found that all instances of
> "substring\n" were being replaced by "substring(carriage return)\n"
> after post.
>
> For now, I'm simply doing str_replace(chr(13), "", $_POST['value'])
> before re-inserting it into the HTML, but I was wondering why PHP is
> inserting those extra characters.

I don't know that this will help, but maybe it will provide a clue.
A textarea field will insert CRLF in its posted contents. You might
expect this to be governed by the platform the browser is running on,
but no. It inserts CRLF anyway.

Like I said, just a clue.

Paul

--
Paul M. Foster

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: PHP inserting carriage returns into POST values?

am 05.09.2009 00:14:29 von James Colannino

Nisse Engström wrote:

> It may be the browser that is converting those line breaks.

Ah. That's probably it then. I didn't realize that was a part of the
HTML standard. Thanks!

James

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php