简体   繁体   中英

replace <br /> with “\n” in fpdf

I'm creating a pdf dynamically with fpdf. The code might not be the prettiest but it works more or less like I want. The problem I have is that I want to replace <br /> in everything that is fetched from the database with "\\n" . Whenever I try to add and variables myself I get error messages with

Unexpected variable in...

My full code is this:

<?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();

?>

Again, not the prettiest code but it works. But not if I want to replace <br /> with linebreak. How do I do this?

This should work, put this just before your loop :

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');
    //...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM