Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中从文本区域中检索行时,除了最后一个元素,其余所有元素都添加了一个结束空间_Php_Html_Arrays_Tags_Textarea - Fatal编程技术网

在php中从文本区域中检索行时,除了最后一个元素,其余所有元素都添加了一个结束空间

在php中从文本区域中检索行时,除了最后一个元素,其余所有元素都添加了一个结束空间,php,html,arrays,tags,textarea,Php,Html,Arrays,Tags,Textarea,嗨,朋友们,当我从文本区域获取行并在for循环中打印它们时,除了最后一个元素,所有元素都在添加空格不知道为什么要添加空格,因为我没有添加任何空格 这是代码 <form method="post" enctype="multipart/form-data" action=""> <textarea name="words" id="words" rows="10" cols="100"></textarea><br><br> &

嗨,朋友们,当我从文本区域获取行并在for循环中打印它们时,除了最后一个元素,所有元素都在添加空格不知道为什么要添加空格,因为我没有添加任何空格 这是代码

 <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;
      }
    }

}
?>
为什么在hello和hello之间有空格?有人能帮我解决这个问题吗?

你可以使用rtrim()去掉空格

foreach ($text as $line) {
      for($i=0; $i<3; $i++){
          echo rtrim($line,' ');
      }
    }
foreach($text作为$line){

对于($i=0;$i请在分解步骤中删除
\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
被呈现为空白字符

if (isset($_POST['words_submit'], $_POST['words'])) {
    $text = explode ("\r\n", trim($_POST['words']));
    foreach ($text as $line) {
        echo str_repeat($line, 4);
    }    
}