【问题标题】:Line breaks between variables in PHPPHP中变量之间的换行符
【发布时间】:2018-07-27 11:59:50
【问题描述】:

我在 foreach 中有一个这样的变量

$variable=$variable1."               ".$variable2;

输出是

apple                   fruit
tomato                   fruit
pineapple                   fruit

我想要的是什么

    apple                   fruit
    tomato                  fruit
    pineapple               fruit

我怎样才能实现它?

【问题讨论】:

  • 自己计算需要的空格数。使用第一个文本的长度。
  • 您需要制表吗\t
  • 使用 html 表格!
  • 你也可以使用str_pad()。我怀疑 OP 的结果是否在 HTML 页面中,因为结果中有多个空格。 HTML 只会显示一个空格,无论您在一个之后放置多少个空格,因此使用 HTML 表格可能不合适。

标签: php whitespace spaces


【解决方案1】:

"\t" 不是一个可行的选择,但@Magnus Eriksson 给出了很好的答案:

echo "\t".$var1."\t".$var2.PHP_EOL:

   apple   fruit
   tomato   fruit
   pineapple   fruit

echo ' '.str_pad($var1,20," ").$var2.PHP_EOL:

  apple               fruit
  tomato              fruit
  pineapple           fruit

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-21
    • 2011-11-19
    • 2014-03-07
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    相关资源
    最近更新 更多