使用fpdf:str_pad不适用于空格,它适用于其他字符

时间:2018-01-17 13:42:23

标签: php string fpdf

我正在为我的字符串添加一个填充,以填充空格,但它不起作用 代码在这里

<?php 
    $string1 = "Product 1 ";
    $newString  = str_pad($string1,100);
    echo $newString."test";
    echo "<br>";
    $string2 = "Product 2222 ";
    echo str_pad($string2,100," ")."test";
    echo "<br>";
 ?>

输出如下:

Product 1 test
Product 2222 test

2 个答案:

答案 0 :(得分:1)

您可以尝试使用$str = str_pad($string2,(100*strlen("&nbsp;")),"&nbsp;")."test";

&nbsp;渲染到html中的非破坏空间(以及使用fpdf写入文档时)。

请注意,当您告诉它将所有行都写为html时,这只适用于fpdf!编码应该是utf-8可能

$fpdf->Write(iconv('UTF-8', 'windows-1252', html_entity_decode($str)));

答案 1 :(得分:0)

PHP 的输出转换为 HTML 时,除了第一个之外的所有空白,这是默认功能HTML和Web浏览器。所以输出不正确。
您必须在str_pad函数中使用“”而不是空格。 HTML不会忽略“”并且针对它的每一个存在,HTML会在字符串中添加一个空格。