dousui7410 2016-06-04 16:51
浏览 26

来自textarea的额外角色

I have two pages like this:

test.html

<!DOCTYPE html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
</head>

<body>
<form method="POST" action="test2.php">
    <textarea id="input" name="input" cols="100px" rows="15" wrap="soft" placeholder="put input here"></textarea>
    <button type="button" onclick="getLength()">Get length </button><p id="length"> </p></div>
    <br >
    <input type="submit">
</form>

<script>
function getLength(){
    var len=$('textarea').val().length;
    $('#length').text(len);
}
</script>
</body>
</html>

And test2.php

<?php
echo strlen($_POST['input']);
?>

When I enter some input eg: "aa[pressEnter]aa", It should echo 5 at test2.php page but it echoes 6 instead.

So why it is 6 (what is extra character) ?

thanks for reading!

  • 写回答

1条回答 默认 最新

  • drau89457 2016-06-04 17:30
    关注

    Return is counted as an extra character in chrome.

    Actually return is 2 escape characters . In chrome it takes one character (idk why), in edge it takes it as 2 characters so aa[enter]aa sums up to 6 characters

    EDIT: 2 parts are there, one is js fix for removal of return carriage and one is php fix

    js fix in html file

    <!DOCTYPE html>
    <head>
    
    </head>
    
    <body>
    <form method="POST" action="test2.php">
        <textarea id="input" name="input" cols="100px" rows="15" wrap="soft" placeholder="put input here"></textarea>
        <button type="button" onclick="getLength()">Get length </button><p id="length"> </p></div>
        <br >
        <input type="submit">
    </form>
    
    <script>
    function getLength(){
    
        var fixedText = $('textarea').val();
    
        fixedText = fixedText.replace(/(
    |
    |)/gm, "");
    
        var len=fixedText.length;
    
        $('#length').text(len);
    }
    </script>
    </body>
    </html>
    

    php fix

    <?php
    
        $data = str_replace("
    ",'',$_POST['input']);
        echo strlen($data);
    
    ?>
    

    You just need to replace the return carriages , and with "".

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP