douluo3256 2016-10-25 18:31
浏览 190
已采纳

PHP - 从Textarea中删除换行符

I was wondering what I am missing in my code. I have a large form that is pushing all values to a .csv file. There are instances of textareas and every time I put some text content in and add a line break (hit the enter-key) within the .csv document, any line of text after the first breaks the flow of the values within the .csv, and starts a new line.

I've tried checking taking the value via php and removing any spaces or breaks, but it doesn't seem to be working.

Can anyone tell me what I am missing?

HTML:

<textarea id="fianlCommentsText" name="fianlCommentsText"></textarea>

PHP:

$finalCommentsText = $_POST["finalCommentsText"];
function finalCommentsLineCheck()
    {
        global $finalCommentsText;
        preg_replace( "/|
/", "", $finalCommentsText );
    }
    finalCommentsLineCheck();
  • 写回答

3条回答 默认 最新

  • dongliyun3301 2016-10-25 18:36
    关注

    The "choice" of or requires parens:

    preg_replace( "/(|
    )/", "", $finalCommentsText );
    

    Try that?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dt56449492 2016-10-25 18:37
    关注

    In order to allow multiline fields in csv, you need to quote them with "

    For example:

    12345,"multiline
    text",another_field
    

    In case you have quotes inside your text, just escape them with another quote:

    12345,"27"" monitor 
    TFT",another_field
    

    This way you can keep the newlines inside the text.

    评论
  • dte29947 2017-03-31 15:51
    关注

    In my case I solved in this way:

    Output to a textarea removing the ' ' characters:

    <!-- this textarea will show blank carriage return and not 
     characters -->
    <textarea  class="settinginput" id="message" name="message">
    <?php echo str_replace('
    ',"
    ",$message); ?>
    </textarea>
    

    After submit here is how to replace the carriage returns with the ' ' not expanded characters:

    str_replace("
    ",'
    ',$_POST['message'])
    

    Note that the replace function uses double quotes and single quotes in order to save the string with no expansion for escape sequences.

    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 grafna发送告警信息
  • ¥15 51单片机,LCD屏幕内容修改
  • ¥20 Ida Pro动态调试
  • ¥15 TensorFlow深度学习拓展项目
  • ¥20 springboot博客系统
  • ¥15 MICE包多重插补后数据集汇总导出
  • ¥15 一道算法分析问题(关于3-MSAT)
  • ¥15 C++ FLUENT 化学反应速率 编写困难
  • ¥15 Python嵌套交叉验证
  • ¥15 linuxkit+elasticsearch