使用$ _GET字符串传递换行符

问题描述:

I have to pass a string to a form. I want to use a mix of $_GET and $_POST in the following way:

 <?php $string="bla bla bla".$some_other_string."bla bla".$some_other_string2."
"; ?>

 <form action="this_page.php?string=<?php echo $string?>" method="post" name="name">

 </form>

Please note the in the string. When I get the $_GET("string") (or $_REQUEST("string"), it happens the following: the php parser get the as a string. So he put it into the string as I had coded: \\ and it's not the result I want. I want simply a inner in a php string.

Please can you explain me why this behaviour happens, without complaining that it's not the best way to pass a string (I suppose it isn't, but today I had an issue and I wanted to manage it by passing the string this way)?

Ps: I have to use the and not the html <br /> because I have to let php write into a file.

我必须将一个字符串传递给一个表单。 我想使用$ _GET和$ _POST的混合 通过以下方式: p>

 &lt;?php $ string =“bla bla bla”。$ some_other_string。“bla bla”。$ some_other_string2。“
”;  ?&gt; 
 
&lt; form action =“this_page.php?string =&lt;?php echo $ string?&gt;”  method =“post”name =“name”&gt; 
 
&lt; / form&gt; 
  code>  pre> 
 
 

请注意 code> 在字符串中。 当我得到 $ _ GET(“string”) code>(或 $ _ REQUEST(“string” code>)时,会发生以下情况:php解析器获取 作为一个字符串。所以他把它放入字符串中,因为我编码: \\ code>,这不是我想要的结果。我只想要一个内部 code >在一个php字符串中。 p>

请你解释一下为什么会发生这种情况,而不是抱怨它不是传递字符串的最佳方式(我想它不是,但今天我 有一个问题,我想通过这种方式传递字符串来管理它?? p>

Ps:我必须使用 code>而不是html &lt; br /&gt; code>因为我必须让php写入文件。 p> div>

Advice: You probably should think about putting this into a hidden field

Answer: But if you are going to put it into action as a GET parameter, then you need to urlencode the string:

<form action="this_page.php?string=<?php echo urlencode($string); ?>" method="post" name="name">

That way - any non-alphanumeric characters will be url safe (think about what might happen if $string contained a ? or a # or an = ).

PHP automatically decodes GET parameters when loading them into the $_GET array - so your newline should be preserved at that point.