显示在文件行内容之前的空格 - PHP(Display white spaces that precede the content of the line of the file - PHP)
我目前正在使用foreach循环来显示文本文件的内容。 但是,我还希望显示在行的实际内容之前的空格。 怎么办?
$loop_var = 0; foreach($lines as $line) { $loop_var++; if ($loop_var == 1) { echo'<div id="h1">' . $line . '</div>'; } if ($loop_var == 2) { echo '<div id="h2">' . $line . '</div><br />'; } if ($loop_var > 2) { if ($loop_var == 3) { echo '<pre><div id="code">'; } echo ($line) . "<br />"; } } echo '</pre></div>';现在,如果文本文件包含以下内容:
blah blah blah blah它显示为:
blah blah blah blahI'm currently using foreach loop to display the contents of a text file. However, I want to also display the whitespaces that precede the actual content of the line. How to do so?
$loop_var = 0; foreach($lines as $line) { $loop_var++; if ($loop_var == 1) { echo'<div id="h1">' . $line . '</div>'; } if ($loop_var == 2) { echo '<div id="h2">' . $line . '</div><br />'; } if ($loop_var > 2) { if ($loop_var == 3) { echo '<pre><div id="code">'; } echo ($line) . "<br />"; } } echo '</pre></div>';Now, if the textfile contains the following:
blah blah blah blahIt is getting displayed as:
blah blah blah blah最满意答案
使用<pre>标签,并将textfile的内容打印到此。 例:
print '<pre>'.file_get_contents('filename.txt').'</pre>';逐行(有条件)
$file = fopen('filename','r'); print '<pre>'; $counter = 0; while( $line = fgets($file) ){ if( /*the condition comes here whitch line you want to print. example: */ $counter >= 2 ){ print $line; } if( /*the condition comes here where wants you end the printing. example: */ $counter >= 10 ) $counter++; } print '</pre>'; fclose($file);Use <pre> tag, and print the content of textfile into this. example:
print '<pre>'.file_get_contents('filename.txt').'</pre>';Line By line (With conditions)
$file = fopen('filename','r'); print '<pre>'; $counter = 0; while( $line = fgets($file) ){ if( /*the condition comes here whitch line you want to print. example: */ $counter >= 2 ){ print $line; } if( /*the condition comes here where wants you end the printing. example: */ $counter >= 10 ) $counter++; } print '</pre>'; fclose($file);更多推荐
发布评论