FPDF - WriteHTML 函数中的 PHP?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9472660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FPDF - PHP inside WriteHTML function?
提问by Norm
I have inserted some text in the WriteHTML function provided by http://www.fpdf.org/under 'Tutorial 6: Links and flowing text'.
我在http://www.fpdf.org/提供的 WriteHTML 函数中插入了一些文本,位于“教程 6:链接和流动文本”下。
I want to add some php code inside of it but it does not quite function.
我想在其中添加一些 php 代码,但它不起作用。
I tried the following but it just wrote the code too.
我尝试了以下但它也只写了代码。
while ($row = mysql_fetch_array($result)) {
$html='Person:<b>".$row["firstname"] ." ". $row["lastname"]."</b>';
$pdf->AddPage();
$pdf->SetXY(12, 127);
$pdf->SetFontSize(11);
$pdf->WriteHTML(utf8_decode($html));
} $pdf->Output();
Any ideas? Is there any function in existance that can make this work?
有任何想法吗?是否有任何功能可以使这项工作?
采纳答案by enderskill
Always be consistent with the use of quotes. If you open with a single-quote, close with a single-quote and if you open with a double-quote, close with a double-quote. Change the code to the following and it should work.
始终与引号的使用保持一致。如果用单引号打开,用单引号关闭,如果用双引号打开,用双引号关闭。将代码更改为以下内容,它应该可以工作。
while ($row = mysql_fetch_array($result)) {
$html="Person:<b>".htmlspecialchars($row["firstname"])." ".htmlspecialchars($row["lastname"])."</b>";
$pdf->AddPage();
$pdf->SetXY(12, 127);
$pdf->SetFontSize(11);
$pdf->WriteHTML(utf8_decode($html));
} $pdf->Output();
回答by Josh
Since everyone is ignoring my comments, I'll submit my own answer:
由于大家都无视我的评论,我将提交我自己的答案:
while ($row = mysql_fetch_array($result)) {
$html="Person:<b>".htmlspecialchars($row["firstname"])." ".
htmlspecialchars($row["lastname"])."</b>";
$pdf->AddPage();
$pdf->SetXY(12, 127);
$pdf->SetFontSize(11);
$pdf->WriteHTML(utf8_decode($html));
}
$pdf->Output();
Using htmlspecialchars
ensures that names like O'Donnel
or, worse, Mallory<evil code here>
will not create invalid/dangerous HTML.
使用htmlspecialchars
可确保名称之类的,O'Donnel
或者更糟的是,Mallory<evil code here>
不会创建无效/危险的 HTML。
Note that what actually solves your problem is mismatched quotes:
请注意,真正解决您的问题的是不匹配的引号:
$html='Person:<b>".$row["firstname"] ." ". $row["lastname"]."</b>';
verses:
诗句:
$html="Person:<b>".$row["firstname"] ." ". $row["lastname"]."</b>";
回答by cb1
If you change the setting of your $html
variable to the following, your code should work correctly:
如果您将$html
变量的设置更改为以下内容,您的代码应该可以正常工作:
$html = 'Person:<b>' . $row['firstname'] . ' ' . $row['lastname'] . '</b>';
You were mixing single and double quotes incorrectly in your example.
您在示例中错误地混合了单引号和双引号。
The other option is to use double quotes, which allows variables to be parsed inside the quotes.
另一种选择是使用双引号,它允许在引号内解析变量。
$html = "Person:<b> {$row['firstname']} {$row['lastname']} </b>";