在 php 中使用 \t 格式化列中的纯文本值

发布于 2024-11-13 03:05:33 字数 1255 浏览 2 评论 0原文

我在 php 中有以下代码,当我执行它时:

<pre>
<?php
$input = '<xml>
<user>
    <name>sujit agarwal</name>
    <age>22</age>
    <gender>male</gender>
</user>
<user>
    <name>ajay rana</name>
    <age>25</age>
    <gender>male</gender>
</user>
<user>
    <name>pratap singh</name>
    <age>27</age>
    <gender>male</gender>
</user>
<user>
    <name>asdfasdfasdfasdf</name>
    <age>30</age>
    <gender>male</gender>
</user>
</xml>';
$sx = simplexml_load_string($input);
foreach($sx as $val)
echo $val->name."\t".$val->gender."\t".$val->age."\n";
?>
</pre>

输出是

sujit agarwal   male    22
ajay rana   male    25
pratap singh    male    27
asdfasdfasdfasdf    male    30

现在,我的问题是如何对齐列中的所有性别值和年龄值,即使名称比其他值长。希望我说清楚了我的问题。 这将在 PRE 标签内完成。因为我稍后会将其保存到 TXT 文件

需要格式化

sujit agarwal       male    22
ajay rana           male    25
pratap singh        male    27
asdfasdfasdfasdf    male    30

I have the following code in php, when i execute it:

<pre>
<?php
$input = '<xml>
<user>
    <name>sujit agarwal</name>
    <age>22</age>
    <gender>male</gender>
</user>
<user>
    <name>ajay rana</name>
    <age>25</age>
    <gender>male</gender>
</user>
<user>
    <name>pratap singh</name>
    <age>27</age>
    <gender>male</gender>
</user>
<user>
    <name>asdfasdfasdfasdf</name>
    <age>30</age>
    <gender>male</gender>
</user>
</xml>';
$sx = simplexml_load_string($input);
foreach($sx as $val)
echo $val->name."\t".$val->gender."\t".$val->age."\n";
?>
</pre>

THE OUTPUT IS

sujit agarwal   male    22
ajay rana   male    25
pratap singh    male    27
asdfasdfasdfasdf    male    30

now, my problem is how to align all the gender values and age values in a column even if a name is longer than the rest. Hope i made my problem clear. This is to be done within the PRE tags. because i will later save this to a TXT file

NEEDED FORMATTING

sujit agarwal       male    22
ajay rana           male    25
pratap singh        male    27
asdfasdfasdfasdf    male    30

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

并安 2024-11-20 03:05:33
<?php
$input = '<xml>
<user>
    <name>sujit agarwal</name>
    <age>22</age>
    <gender>male</gender>
</user>
<user>
    <name>ajay rana</name>
    <age>25</age>
    <gender>male</gender>
</user>
<user>
    <name>pratap singh</name>
    <age>27</age>
    <gender>male</gender>
</user>
<user>
    <name>asdfasdfasdfasdf</name>
    <age>30</age>
    <gender>male</gender>
</user>
</xml>';
$sx = simplexml_load_string($input);

$output='';
foreach($sx as $val){
    $values[] = $val->name;
    $values[] = $val->gender;
    $values[] = $val->age;
    $output.=vsprintf("%-30s %s\t %d",$values)."\n";
    $values='';
}

echo '<pre>';
echo $output;
echo '</pre>';
?>

输出:

sujit agarwal                  male  22
ajay rana                      male  25
pratap singh                   male  27
asdfasdfasdfasdf               male  30
<?php
$input = '<xml>
<user>
    <name>sujit agarwal</name>
    <age>22</age>
    <gender>male</gender>
</user>
<user>
    <name>ajay rana</name>
    <age>25</age>
    <gender>male</gender>
</user>
<user>
    <name>pratap singh</name>
    <age>27</age>
    <gender>male</gender>
</user>
<user>
    <name>asdfasdfasdfasdf</name>
    <age>30</age>
    <gender>male</gender>
</user>
</xml>';
$sx = simplexml_load_string($input);

$output='';
foreach($sx as $val){
    $values[] = $val->name;
    $values[] = $val->gender;
    $values[] = $val->age;
    $output.=vsprintf("%-30s %s\t %d",$values)."\n";
    $values='';
}

echo '<pre>';
echo $output;
echo '</pre>';
?>

outputs:

sujit agarwal                  male  22
ajay rana                      male  25
pratap singh                   male  27
asdfasdfasdfasdf               male  30
拔了角的鹿 2024-11-20 03:05:33

我只需要快速将其敲入 shell 脚本即可,该方法相当可移植。

只需传递一个元素数组,并将列选项卡分开 - 它将返回一个格式化的字符串,其中列的间距适当。

function function formatCols($rows)
{
  $colwidth = array();
  $rowsParts = array();
  $results = null;
  foreach ($rows as $row) {
    $cols = explode("\t", $row);
    $rowsParts[] = $cols;
    foreach ($cols as $id => $col)
      if (!isset($colwidth[$id]) || strlen($col) > $colwidth[$id])
        $colwidth[$id] = strlen($col);
  }
  $colFormat = null;
  for ($i=0; $i<count($colwidth); $i++)
    $colFormat .= '%-'.$colwidth[$i].'s ';
  $colFormat = trim($colFormat)."\n";
  foreach ($rowsParts as $row)
    $results .= vsprintf($colFormat, $row);
  return $results;
}

I just had to knock this up quickly for a shell script, the method is fairly portable.

Just pass an array of elements, with the columns tab separated - and it will return a formatted string with the columns properly spaced.

function function formatCols($rows)
{
  $colwidth = array();
  $rowsParts = array();
  $results = null;
  foreach ($rows as $row) {
    $cols = explode("\t", $row);
    $rowsParts[] = $cols;
    foreach ($cols as $id => $col)
      if (!isset($colwidth[$id]) || strlen($col) > $colwidth[$id])
        $colwidth[$id] = strlen($col);
  }
  $colFormat = null;
  for ($i=0; $i<count($colwidth); $i++)
    $colFormat .= '%-'.$colwidth[$i].'s ';
  $colFormat = trim($colFormat)."\n";
  foreach ($rowsParts as $row)
    $results .= vsprintf($colFormat, $row);
  return $results;
}
她如夕阳 2024-11-20 03:05:33

这并不太难。

您要做的就是计算出每列的“宽度”。然后计算出需要附加多少个选项卡。
因此,您需要将所有行循环两次。首先确定每列的最宽值是多少,然后“打印”数据以及适量的制表符。

或者您可以只使用 HTML 表来为您处理它,但我可以理解这不是一个选项。

This isn't too hard.

What you have to do is figure out how 'wide' each column is. And then figure out how many tabs you need to append to it.
So you will need to loop through all the rows twice. First to determine for each column what the widest value is and then to 'print' the data plus the right amount of tabs.

Or you could just use HTML tables which takes care of it for you but I can understand that it is not an option.

心碎无痕… 2024-11-20 03:05:33

您必须循环两次:第一次建立每个单独字段的最大长度,第二次用那么多空白写出它(对此我建议使用 vsprintf('%45s %7s %-3d',$args);,或任何值。

You'll have to loop twice: 1 to establish the max length of each individual field, and a second one to write it out with that much whitespace (for which I'd recommend something like vsprintf('%45s %7s %-3d',$args);, or whatever the values may be.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文