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

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;
?>
  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?