使用 $_GET 字符串传递换行符
我必须将字符串传递给表单。 我想按以下方式混合使用 $_GET 和 $_POST:
<?php $string="bla bla bla".$some_other_string."bla bla".$some_other_string2."\n"; ?>
<form action="this_page.php?string=<?php echo $string?>" method="post" name="name">
</form>
请注意字符串中的 \n
。 当我得到 $_GET("string")
(或 $_REQUEST("string"
))时,会发生以下情况:php 解析器将 \n 作为字符串获取所以他将其放入我编码的字符串中: \\\n
这不是我想要的结果,我只是想要一个 php 字符串中的内部 \n
。 。
请你解释一下为什么会发生这种行为,而不 抱怨这不是传递字符串的最佳方式(我想不是,但今天我遇到了问题,我想通过这种方式传递字符串来管理它)?
Ps:我必须使用 \ n
而不是 html
因为我必须让 php 写入文件。
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."\n"; ?>
<form action="this_page.php?string=<?php echo $string?>" method="post" name="name">
</form>
Please note the \n
in the string.
When I get the $_GET("string")
(or $_REQUEST("string"
), it happens the following: the php parser get the \n as a string. So he put it into the string as I had coded: \\\n
and it's not the result I want. I want simply a inner \n
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 \n
and not the html <br />
because I have to let php write into a file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
建议:您可能应该考虑将其放入隐藏字段
答案:但是如果您要将其作为 GET 参数付诸实践,那么您需要对string:
这样 - 任何非字母数字字符都是 url 安全的(想想如果 $string 包含 ? 或 # 或 = 会发生什么)。
当将 GET 参数加载到 $_GET 数组中时,PHP 会自动解码它们 - 因此此时应该保留换行符。
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:
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.