如何在PHP中换行?

时间:2018-01-16 11:14:28

标签: php newline

您好朋友我点击提交表单后无法在换行符中打印即使我已经使用了#34; \ r \ n&#34;这是我的MWE for html coding < / p>

 <html>

    <head>

    <script type="text/javascript" src="validate.js"></script>

    </head>

    <body>

    <form name="StudentRegistration" action="proces.php" method="post">


  <table cellpadding="2" width="20%" bgcolor="99FFFF" align="center"  cellspacing="2">

  <tr>

    <td colspan=2>

    <center><font size=4><b>Student Registration Form</b></font></center>

    </td>

    </tr>

 <tr>

 </tr>
 <tr>

 </tr>
 <tr>

 </tr>
 <tr>

 </tr>

  <tr>

    <td>Name</td>

    <td><input type=text name=textnames id="textname" size="30"></td>

    </tr>

  <tr>

    <td>Father Name</td>

    <td><input type="text" name="fathername" id="fathername" size="30"></td>

    </tr>

    <tr>

    <td>Mother Name</td>

    <td><input type="text" name="mothername" id="mothername" size="30"></td>

    </tr>

   <tr>

      <td>Institute</td>

      <td><select name="institute">

      <option value=" " selected>Select..</option>

      <option value="Institute-1">Institute-1</option>

      <option value="Institute-2">Institute-2</option>

      </select></td>

    </tr>


    <td>Category</td>

      <td><select name="category">

      <option value=" " selected>Select..</option>

      <option value="Regular">Regular</option>
      <option value="Private">Private</option>

      </select></td>

    </tr>

  <tr>

    <td>Sex</td>

    <td><input type="radio" name="sex" value="Male" size="10">Male

    <input type="radio" name="sex" value="Female" size="10">Female</td>

    </tr>

    <td>Stream</td>

    <td><select name="stream">

    <option value=" " selected>Select..</option>

    <option value="ARTS">ARTS</option>

    <option value="COMMERCE">COMMERCE</option>

    <option value="SCIENCE">SCIENCE</option>

    </select></td>

    </tr>

  <tr>

    <td>Core-1</td>

    <td><select name="core-1">

    <option value=" " selected>Select..</option>

    <option value="ENGL">English</option>

    </select></td>

    </tr>
  <tr>

    <td>Core-2</td>

    <td><select name="core-2">

    <option value=" " selected>Select..</option>

    <option value="ALTE">Alternative English</option>

    </select></td>

    </tr>

  <tr>

  <td>Elective-1</td>

    <td><select name="elective-1">

    <option value=" " selected>Select..</option>
    <option value="HIST">History</option>
    <option value="LOPH">Logic & Philosophy</option>
    <option value="MATH">Mathematics</option>
    <option value="MBEN">MIL (Bengali)</option>
    <option value="MBOD">MIL (Bodo)</option>
    <option value="MHIN">MIL (Hindi)</option>
    <option value="MNEP">MIL (Nepali)</option>
    <option value="PHYS">Physics</option>
    <option value="POSC">Political Science</option>
    <option value="SOCI">Sociology</option>
    <option value="STAT">Statistics</option>
    </select></td>

    </tr>

    <tr>

    <td>Elective-2</td>

      <td><select name="elective-2">

    <option value=" " selected>Select..</option>
    <option value="HIST">History</option>
    <option value="LOPH">Logic & Philosophy</option>
    <option value="MATH">Mathematics</option>
    <option value="MBEN">MIL (Bengali)</option>
    <option value="MBOD">MIL (Bodo)</option>
    <option value="MHIN">MIL (Hindi)</option>
    <option value="MNEP">MIL (Nepali)</option>
    <option value="PHYS">Physics</option>
    <option value="POSC">Political Science</option>
    <option value="SOCI">Sociology</option>
    <option value="STAT">Statistics</option>
    </select></td>

    </tr>

    <tr>

    <td>Elective-3</td>

      <td><select name="elective-3">
    <option value=" " selected>Select..</option>
    <option value="HIST">History</option>
    <option value="LOPH">Logic & Philosophy</option>
    <option value="MATH">Mathematics</option>
    <option value="MBEN">MIL (Bengali)</option>
    <option value="MBOD">MIL (Bodo)</option>
    <option value="MHIN">MIL (Hindi)</option>
    <option value="MNEP">MIL (Nepali)</option>
    <option value="PHYS">Physics</option>
    <option value="POSC">Political Science</option>
    <option value="SOCI">Sociology</option>
    <option value="STAT">Statistics</option>
    </select></td>

    </tr>

    <tr>

    <td>Elective-4</td>

      <td><select name="elective-4">

    <option value=" " selected>Select..</option>
    <option value="HIST">History</option>
    <option value="LOPH">Logic & Philosophy</option>
    <option value="MATH">Mathematics</option>
    <option value="MBEN">MIL (Bengali)</option>
    <option value="MBOD">MIL (Bodo)</option>
    <option value="MHIN">MIL (Hindi)</option>
    <option value="MNEP">MIL (Nepali)</option>
    <option value="PHYS">Physics</option>
    <option value="POSC">Political Science</option>
    <option value="SOCI">Sociology</option>
    <option value="STAT">Statistics</option>
    </select></td>

    </tr>

    <tr>

    <td>Compulsory</td>

      <td><select name="compulsory">

      <option value=" " selected>Select..</option>

        <option value="ENVE">Environmental Education</option>

      </select></td>

      </tr>




     <tr>

      <td><input type="reset"></td>


      <td colspan="2"><input type="submit" value="Submit Form" /></td>

      </tr>

      </table>

      </form>

      </body>

      </html>

这是用于将数据导出到.csv文件以保存数据的php的MWE,我希望每个提交表单我想要在换行符中打印数据

 <?php
 $fieldAA = $_POST[" "];
 $fieldA = $_POST["textnames"];
 $fieldB = $_POST["fathername"];
 $fieldC = $_POST["mothername"];
 $fieldD = $_POST["institute"];
 $fieldE = $_POST["category"];
 $fieldF = $_POST["sex"];
 $fieldG = $_POST["stream"];
 $fieldH = $_POST["core-1"];
 $fieldI = $_POST["core-2"];
 $fieldJ = $_POST["elective-1"];
 $fieldK = $_POST["elective-2"];
 $fieldL = $_POST["elective-3"];
 $fieldM = $_POST["elective-4"];
 $fieldN = $_POST["compulsory"];

 $keys = array($fieldAA,$fieldA,$fieldB,$fieldC,$fieldD,$fieldE,$fieldF,$fieldG,$fieldH,'','',$fieldI,'','',$fieldJ,'','',$fieldK,'','',$fieldL,'','',$fieldM,'','',$fieldN,''); //THIS IS WHERE YOU PUT THE FORM ELEMENTS ex: array('$fieldA','$fieldB',etc)
 $csv_line = $keys;
 foreach( $keys as $key ){
     array_push($csv_line,'' . $_GET[$key]);

 }
 $fname = 'data.csv'; //NAME OF THE FILE
 $csv_line = implode(',',$csv_line);
 if(!file_exists($fname)){$csv_line = "\r\n" . $csv_line ;}
 $fcon = fopen($fname,'a');
 $fcontent = $csv_line ;
 fwrite($fcon,$csv_line);
 fclose($fcon);
 echo "Successfully done! $keys[1]"

 ?>

1 个答案:

答案 0 :(得分:1)

在php中我们可以使用PHP_EOL来回显一行的结尾,使用此方法将不依赖于当前文档/平台的行结尾。

实施例: -

$string = "I want a new line here" . PHP_EOL . "This is my new line";

csvs中的换行符取决于您用来查看它们的内容。

此外,您可能想要解释MWE在您的问题中的含义。