显示在文件行内容之前的空格

编程入门 行业动态 更新时间:2024-10-26 17:33:32
显示在文件行内容之前的空格 - 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 blah

I'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 blah

It 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);

更多推荐

本文发布于:2023-08-02 23:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382510.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:空格   文件   内容

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!