在 php 中使用 \t 格式化列中的纯文本值
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
输出:
outputs:
我只需要快速将其敲入 shell 脚本即可,该方法相当可移植。
只需传递一个元素数组,并将列选项卡分开 - 它将返回一个格式化的字符串,其中列的间距适当。
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.
这并不太难。
您要做的就是计算出每列的“宽度”。然后计算出需要附加多少个选项卡。
因此,您需要将所有行循环两次。首先确定每列的最宽值是多少,然后“打印”数据以及适量的制表符。
或者您可以只使用 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.
您必须循环两次:第一次建立每个单独字段的最大长度,第二次用那么多空白写出它(对此我建议使用
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.