无法从文本区域中删除空格

发布于 2024-12-25 01:48:21 字数 708 浏览 6 评论 0原文

我有两个文件。第一个文件是表单,第二个文件显示结果。

第一个文件:

<form action="show.php" method=post>
<textarea name="test" cols=40 rows=6>
</textarea>
<input type=submit value="submit">
</form>

第二个文件:

<?php
$text = trim($_POST['test']);
$textAr = explode("\n", $text);

for ($i=0; $i<sizeof($textAr); $i++)
{
    $link = str_replace(" ", "", $textAr[$i]);
    echo $link."---<br>";
}
?>

例如,当我输入表单时,

one
two
three

结果是:

one ---
two ---
three---

在字符串的末尾:一和二有我无法删除的空格。有人能帮我解决这个问题吗?

谢谢我使用了 $link = str_replace("\r", "", $textAr[$i]) 现在一切都好

I have two files. First file is a form and the second file displays result.

First file:

<form action="show.php" method=post>
<textarea name="test" cols=40 rows=6>
</textarea>
<input type=submit value="submit">
</form>

Second file:

<?php
$text = trim($_POST['test']);
$textAr = explode("\n", $text);

for ($i=0; $i<sizeof($textAr); $i++)
{
    $link = str_replace(" ", "", $textAr[$i]);
    echo $link."---<br>";
}
?>

When I enter in the form for example

one
two
three

the result is:

one ---
two ---
three---

at the end on the strings: one and two there are whitespaces that I can't remove. Can anybody help me with this?

Thanks I used $link = str_replace("\r", "", $textAr[$i]) and now everything is ok

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

内心激荡 2025-01-01 01:48:21

使用 trim() 代替 str_replace

Use trim() instead of str_replace

非要怀念 2025-01-01 01:48:21

您可以使用 trim 函数而不是使用 str_replace 来这样做。

You can use trim function instead of using str_replace to do that.

耶耶耶 2025-01-01 01:48:21

基本上,textarea 的换行符在 Internet Explorer 中为 \r\n,在所有其他浏览器(包括 Mac 浏览器)中为 \n。 IE 从版本 9 开始也使用 \n

有多种方法可以修复您的代码。例如,您可以尝试 preg_split()< /a> :

preg_split('/(?:\r\n|\n)/', $text);

但是,我还没有测试过这个提案的性能。

另外,非常重要的是,一定要在正则表达式中将 \r\n 放在 \n 之前!

此外, trim() 更重要适合于 str_replace() (当然,如果您仍然需要它)。

Basically, textarea's linebreaks are \r\n in Internet Explorer, and \n in all other browsers (including Mac's). IE uses \n too since version 9.

There are numerous ways to fix your code. For example, you could try preg_split() :

preg_split('/(?:\r\n|\n)/', $text);

However, I haven't tested the performances of this proposal.

Also, very important, be sure to put the \r\n before the \n in the regex!

Besides, trim() is way more suited than str_replace() (of course if you still need it).

獨角戲 2025-01-01 01:48:21

我知道已经晚了,但可能会帮助其他人。

当需要文档缩进时使用它。

$(document).ready(function()
{
    $('textarea').each(function(){
            $(this).val($(this).val().trim());
        }
    );
});

I know its late but may help others.

use this in when document indentation is required.

$(document).ready(function()
{
    $('textarea').each(function(){
            $(this).val($(this).val().trim());
        }
    );
});
迷途知返 2025-01-01 01:48:21

也许它是非空格空白,例如非换行空格(nbsp)或换行符组合的某些部分?

Maybe it's non space whitespaces like non-break-spaces (nbsp) or some part of a linebreak combination?

甜扑 2025-01-01 01:48:21

尝试 nl2br() 函数,可以是回车符或换行符 (\r\n 、\n\r、\n 和 \r)。

Try nl2br() function, could be carriage return or new line (\r\n, \n\r, \n and \r).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文