gpt4 book ai didi

php - 在 fpdf 中将
替换为 "\n"

转载 作者:行者123 更新时间:2023-11-29 02:49:48 25 4
gpt4 key购买 nike

我正在使用 fpdf 动态创建 pdf。代码可能不是最漂亮的,但它或多或少像我想要的那样工作。我遇到的问题是我想替换 <br />在使用 "\n" 从数据库中获取的所有内容中.每当我尝试自己添加和变量时,我都会收到错误消息

Unexpected variable in...

我的完整代码是这样的:

<?php
//connection to database
require("db_link.inc.php");
//get correct Id
if(isset($_GET["Id"])){
$lpId = $_GET["Id"];
}
else {
header("Location: main.php");
}
//query
$sql_result = $link->query("SELECT * FROM LessonPlans WHERE Id=$lpId;") or die ("<div class='row'>
<div class='col s12 m6 offset-m3'>
<div class='card red'>
<div class='card-content white-text'>
<span class='card-title'><strong>Oh snap!</strong></span>
<p>We couldn&apos;t ask the query</p>
</div>
</div>
</div>");

require('../mlslp/assets/fpdf/fpdf.php');

class PDF extends FPDF

{
// Page footer
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}

// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
// Logo
$pdf->Image('../mlslp/assets/img/ML-logo.jpeg',7,6,30);
// Arial bold 18
$pdf->SetFont('Arial','B',18);
// Move to the right
$pdf->Cell(55);
// Title
while ($row = mysqli_fetch_array($sql_result)){
$pdf->Cell(80,20,$row['Subject'],0,0,'C');
// Move to the right
$pdf->Cell(25);
// Arial 12
$pdf->SetFont('Arial','',12);
// Level
$pdf->Cell(30,20,$row['Level'],0,0,'R');
// Line break
$pdf->Ln(25);
// Arial bold 12
$pdf->SetFont('Arial', 'B', 12);
// Background color
$pdf->SetFillColor(200,220,255);
// Aim heading
$pdf->Cell(0,10,'Aim',0,1,'C',true);
$pdf->Ln(6);
// Times 12
$pdf->SetFont('Times', '', 12);
// Aim
$pdf->Cell(0,0,$row['Aim'],0,1,'C');
$pdf->Ln(10);
// Arial Bold 12
$pdf->SetFont('Arial', 'B', 12);
// Grammar heading
$pdf->Cell(95,10,'Grammar',0,0,'C',true);
// Vocabulary heading
$pdf->Cell(95,10,'Vocabulary',0,1,'C',true);
$pdf->Ln(6);
// Times 12
$pdf->SetFont('Times', '', 12);
// Grammar
$x = $pdf->GetX();
$y = $pdf->GetY();
$pdf->MultiCell(90,7,$row['Grammar'],0,'L');
// Move to the right
$pdf->SetXY($x + 100, $y);
// Vocabulary
$pdf->MultiCell(90,7,$row['Vocabulary'],0,'L');
$pdf->Ln(10);
// Arial Bold 12
$pdf->SetFont('Arial', 'B', 12);
// Procedure heading
$pdf->Cell(0,10,'Procedure',0,1,'C',true);
$pdf->Ln(6);
// Times 12
$pdf->SetFont('Times', '', 12);
// Procedure
$pdf->MultiCell(190,7,$row['Proc'],0,'L');
$pdf->Ln(10);
// Arial Bold 12
$pdf->SetFont('Arial', 'B', 12);
// Exercise heading
$pdf->Cell(0,10,'Exercise',0,1,'C',true);
$pdf->Ln(6);
// Times 12
$pdf->SetFont('Times', '', 12);
// Exercise
$pdf->MultiCell(190,7,$row['Exercise'],0,'L');
}
$pdf->Ln(10);
$pdf->Output();

?>

同样,这不是最漂亮的代码,但它确实有效。但如果我想替换 <br /> 则不行与换行符。我该怎么做?

最佳答案

这应该可以,把它放在你的循环之前:

PHP

$pdf->Cell(55);
$breaks = array("<br />","<br>","<br/>");
while ($row = mysqli_fetch_array($sql_result)){
foreach ($row as $key => $value) {
$row[$key] = str_ireplace($breaks, "\r\n", $value);
}
$pdf->Cell(80,20,$row['Subject'],0,0,'C');
//...

关于php - 在 fpdf 中将 <br/> 替换为 "\n",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37340985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com