无法从文本区域中删除空格
我有两个文件。第一个文件是表单,第二个文件显示结果。
第一个文件:
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用
trim()
代替str_replace
Use
trim()
instead ofstr_replace
您可以使用 trim 函数而不是使用
str_replace
来这样做。You can use trim function instead of using
str_replace
to do that.基本上,
textarea
的换行符在 Internet Explorer 中为\r\n
,在所有其他浏览器(包括 Mac 浏览器)中为\n
。 IE 从版本 9 开始也使用\n
。有多种方法可以修复您的代码。例如,您可以尝试
preg_split()
< /a> :但是,我还没有测试过这个提案的性能。
另外,非常重要的是,一定要在正则表达式中将
\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()
: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 thanstr_replace()
(of course if you still need it).我知道已经晚了,但可能会帮助其他人。
当需要文档缩进时使用它。
I know its late but may help others.
use this in when document indentation is required.
也许它是非空格空白,例如非换行空格(nbsp)或换行符组合的某些部分?
Maybe it's non space whitespaces like non-break-spaces (nbsp) or some part of a linebreak combination?
尝试
nl2br()
函数,可以是回车符或换行符 (\r\n 、\n\r、\n 和 \r)。Try
nl2br()
function, could be carriage return or new line (\r\n, \n\r, \n and \r).