Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.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为文本文件添加300个空格_Php_Whitespace - Fatal编程技术网

使用PHP为文本文件添加300个空格

使用PHP为文本文件添加300个空格,php,whitespace,Php,Whitespace,我想用PHP向文本文件中添加300个空格。我不能使用,因为您已经知道(.txt格式,而不是HTML),所以如何做到这一点 所以这个代码计算数字,正如你们所看到的,我需要300个空格 $spaces = 0; // will become 300 spaces while ($spaces < 300) { $spaces++; } 谢谢,不要创建循环来构建它们,只需使用以下命令 $padded_text = str_pad($some_text, 300, ' ', ST

我想用PHP向文本文件中添加300个空格。我不能使用
,因为您已经知道(.txt格式,而不是HTML),所以如何做到这一点

所以这个代码计算数字,正如你们所看到的,我需要300个空格

$spaces = 0; // will become 300 spaces

 while ($spaces < 300)
 {
    $spaces++;
 }

谢谢,不要创建循环来构建它们,只需使用以下命令

$padded_text = str_pad($some_text, 300, ' ', STR_PAD_LEFT);
任何小于300个字符的大小都会添加空格,根据您想要这些空格的位置,您可以尝试使用STR_PAD_RIGHT或STR_PAD_两者。如果你没有任何字符,它将生成300个空格,这将比使用循环更快

现在,如果您只想添加300个空间(而不是将其填充为少于300个空间),您需要使用

$spaces = str_repeat(' ', 300);

$zerospaces=“hello”$空格=str_pad($zerospaces,300,“,str_pad_LEFT)$spacecount=substr\u count($spaces,“”);echo$spacecount.$spaces
的输出为
295 hello
,并且
count和hello
@F4LLCON之间没有300个空格。如果您只需要300个空格,而不是300个字符的填充文本,则在空变量上运行str_pad,如$spaces=str_pad(“,300,”)@更好的是,如果你实际上没有做任何填充,就用它。@TravisO和Wiseguy谢谢你们,两种解决方案都很有效。我将使用str_repeat(“”,300)@Wiseguy你是对的,我以为他想要填充,然后意识到他只需要300个空格,我更新了上面的答案以反映这两种情况。
$spaces = str_repeat(' ', 300);