[英]Extra character from textarea
我有兩個這樣的頁面:
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>
和test2.php
<?php
echo strlen($_POST['input']);
?>
當我輸入一些輸入,例如: "aa[pressEnter]aa"
,它應該在test2.php頁面上回顯5,但是卻回顯6。
那么為什么是6(多余的字符)呢?
謝謝閱讀!
Return在chrome中算作多余的字符。
實際返回的是2個轉義字符\\ r \\ n。 在chrome中,它需要一個字符(為什么是idk),在邊緣中,它需要2個字符,所以aa [enter] aa總計為6個字符
編輯:有2個部分,一個是js修復,用於去除回車,而一個是php修復
html文件中的js修復
<!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(/(\r\n|\n|\r)/gm, "");
var len=fixedText.length;
$('#length').text(len);
}
</script>
</body>
</html>
PHP修復
<?php
$data = str_replace("\r\n",'',$_POST['input']);
echo strlen($data);
?>
您只需要將回車\\ r \\ n,\\ r和\\ n替換為“”即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.