PHP 读取文件中的空格

huangapple go评论55阅读模式
英文:

PHP read white space in file

问题

I int result = 2 * n;
result = result + 1;

II int result = n + 1;
result = result * 2;

III int result = (n + 1) * 2;

在读取和写入文件时,输出保持额外的空格以保持相同的间距是否有办法?

英文:

I have a file that looks like:

I	    int result = 2 * n;
	    result = result + 1;

II	    int result = n + 1;
	    result = result * 2;

III	    int result = (n + 1) * 2;

I read and write the file with:

while(!feof($myfile)) {
    
    $line =  fgets($myfile) ;
    echo $line . "<br>";
}

the output I get is:

I int result = 2 * n;
result = result + 1;

II int result = n + 1;
result = result * 2;

III int result = (n + 1) * 2;

Is there a way to keep the extra spaces in the text to keep the spacing the same?

答案1

得分: 1

HTML无法保留多个空格,因此用 替换它们:

while(!feof($myfile)) {
    
    $line =  fgets($myfile) ;
    echo str_replace(' ', '&amp;nbsp;', $line) . "<br>";
}
英文:

HTML does not preserve multiple spaces, so replace them with &amp;nbsp;:

while(!feof($myfile)) {
    
    $line =  fgets($myfile) ;
    echo str_replace(&#39; &#39;, &#39;&amp;nbsp;&#39;, $line) . &quot;&lt;br&gt;&quot;;
}

huangapple
  • 本文由 发表于 2023年6月5日 05:14:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定