如何在PHP中创建具有相同字符的x个数字的字符串而不进行循环


How to create a string with x numbers of the same character without looping in PHP

有没有一种很好的方法(无需循环)来创建由多个相同字符构建的字符串?

例如,我知道我应该加5到字符串的开头。如何在不循环的情况下做到这一点?

我事先不知道应该是5英尺,这可能会有所不同。因此,我提出了这个问题。

我在用PHP。

试用str_repeat:

$output = str_repeat(".", 5); // .....

使用str_repeat()函数。例如,要在不使用循环的情况下将5个点连接在一起,可以这样做:

$fiveDots = str_repeat('.', 5);

是的,有.

http://php.net/manual/en/function.str-repeat.php

使用str_pad()函数。。。就这么简单。