【发布时间】:2012-02-06 20:21:01
【问题描述】:
我正在尝试为电子邮件对齐基于文本的项目列表。基本上我遇到的问题是它只适用于固定宽度(等宽)字体 - 我想要一个脚本,它可以根据标准 Arialish 字体中每个字母的宽度以某种方式对齐它。
function sprintf_nbsp() {
$args = func_get_args();
return str_replace(' ', ' ', vsprintf(array_shift($args), array_values($args)));
}
$format = '%-6s%-\'.35.35s...%\'.10s<br>';
$str = sprintf_nbsp($format, '1', 'A list of items - this is the first', '$49.99');
$str .= sprintf_nbsp($format, '100', 'This is something else', '$4.99');
$str .= sprintf_nbsp($format, '5', 'A book', '$499.99');
$str .= sprintf_nbsp($format, '16', 'Testing the function', '$49.99');
echo '<div style="font-family:Courier">'.$str."</div>";
echo '<br><br>'.$str;
(sprintf_nbsp() 可能不是必需的,我只是在 php 论坛上找到的,我对其他解决方案持开放态度)
【问题讨论】:
-
您无法在 PHP 中执行此操作,因为您无法确定 PHP 中每个字母的宽度 - 您需要 CSS 样式或等宽字体来实现所需的输出。
-
那么你建议我如何处理这个问题?
标签: php email printf text-formatting