duangu1878
2017-05-02 00:11
浏览 271
已采纳

php如何在POST变量中转义换行符

I have a form that allows the user to enter what character to trim from the end of a string. Here's a simple example. The form submits back to itself and alters the string Testing 123, If you enter a comma into the form, you get back Testing 123 (comma is removed). But what if that same string had a line break after the comma. I want to be able to enter , into the form and have it return Testing 123 So both the empty line and the comma are trimmed. I assume it has to be escaped, but I can't figure it out. I've tried removeslashes, htmlspecialchars, urldecode. I can't seem to get it.

<?php
if (empty($_POST['trim'])) {
echo "<form method='post' action='".$_SERVER["PHP_SELF"]."'>
<input type='text' name='trim'>
<input type='submit' value='submit'>
</form>";exit;}

$str="testing 123,
";
$str=rtrim($str,$_POST['trim']);
echo $str;
?>

图片转代码服务由CSDN问答提供 功能建议

我有一个表单,允许用户输入要从字符串末尾修剪的字符。 这是一个简单的例子。 表单提交回自身并更改字符串测试123,如果在表单中输入逗号,则返回测试123 (删除逗号)。 但是,如果相同的字符串在逗号后面有换行符 ,该怎么办? 我希望能够在表单中输入并让它返回测试123 因此修剪空行和逗号。 我认为它必须被逃脱,但我无法弄清楚。 我已经尝试过removlashes,htmlspecialchars,urldecode。 我似乎无法得到它。

 &lt;?php 
if(empty($ _ POST ['trim'])){
echo“&lt; form method =  'post'action ='“。$ _ SERVER [”PHP_SELF“]。”'&gt; 
&lt; input type ='text'name ='trim'&gt; 
&lt; input type ='submit'value ='submit'  &gt; 
&lt; / form&gt;“;退出;} 
 
 $ str =”测试123,
“; 
 $ str = rtrim($ str,$ _ POST ['trim']); 
echo $  str; 
?&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doufu9836 2017-05-02 01:34
    已采纳
    $str = rtrim($str);
    $str = rtrim($str, $_POST['trim']);
    

    Will remove whitespace at the end of the string then whatever the user provides.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题