从 php 中的文本区域检索行时,除了最后一个元素之外,所有元素都添加了 space 结尾
While retreiving lines from text area in php except for the last element remaining all elements adding space a end
嗨,朋友们,当我从文本区域获取行并在 for 循环中打印它们时,除了最后一个元素,所有元素都在添加 spaces 不知道为什么要添加 space,因为我没有添加任何 space
这是代码
<form method="post" enctype="multipart/form-data" action="">
<textarea name="words" id="words" rows="10" cols="100"></textarea><br><br>
<input type="submit" name="words_submit" value="Submit keywords">
</form>
<?php
if(isset($_POST['words_submit']))
{
$text = trim($_POST['words']);
$text = explode ("\n", $text);
foreach ($text as $line) {
for($i=0; $i<4; $i++){
echo $line;
}
}
}
?>
假设如果文本区域中有 2 个项目是 hello,welcome 那么它显示输出为
hello hello hello hello welcomewelcomewelcomewelcome
为什么你好之间有 spaces 谁能帮我解决这个问题
您可以使用 rtrim() 去除空格
foreach ($text as $line) {
for($i=0; $i<3; $i++){
echo rtrim($line,' ');
}
}
希望对您有所帮助
在分解步骤中也删除 \r
个字符。
if (isset($_POST['words_submit'], $_POST['words'])) {
$text = explode ("\r\n", trim($_POST['words']));
foreach ($text as $line) {
echo str_repeat($line, 4);
}
}
这假设未呈现的文本是 hello\r\nwelcome
。
在您的代码中,$text
持有 hello\r
和 welcome
。
\r
呈现为空白字符。
嗨,朋友们,当我从文本区域获取行并在 for 循环中打印它们时,除了最后一个元素,所有元素都在添加 spaces 不知道为什么要添加 space,因为我没有添加任何 space 这是代码
<form method="post" enctype="multipart/form-data" action="">
<textarea name="words" id="words" rows="10" cols="100"></textarea><br><br>
<input type="submit" name="words_submit" value="Submit keywords">
</form>
<?php
if(isset($_POST['words_submit']))
{
$text = trim($_POST['words']);
$text = explode ("\n", $text);
foreach ($text as $line) {
for($i=0; $i<4; $i++){
echo $line;
}
}
}
?>
假设如果文本区域中有 2 个项目是 hello,welcome 那么它显示输出为
hello hello hello hello welcomewelcomewelcomewelcome
为什么你好之间有 spaces 谁能帮我解决这个问题
您可以使用 rtrim() 去除空格
foreach ($text as $line) {
for($i=0; $i<3; $i++){
echo rtrim($line,' ');
}
}
希望对您有所帮助
在分解步骤中也删除 \r
个字符。
if (isset($_POST['words_submit'], $_POST['words'])) {
$text = explode ("\r\n", trim($_POST['words']));
foreach ($text as $line) {
echo str_repeat($line, 4);
}
}
这假设未呈现的文本是 hello\r\nwelcome
。
在您的代码中,$text
持有 hello\r
和 welcome
。
\r
呈现为空白字符。