2

I am using PHPExcel to extract data from my database onto an organized Excel sheet. Everything is working great except for one thing. My database entries can sometimes contain HTML markups like <strong></strong>, <BR>, <p></p> etc ... So I managed to get this PHP line working, this is working great to replace my <BR> markups to a space.

$data = str_replace("<br />", "\n", $data);         
$data = str_replace("<br/>", "\n", $data);          
$data = str_replace("<br>", "\n", $data);

However when I try doing the following it does nothing. I was expecting that it would bold the text.

$data = str_replace("<strong>", '&B', $data);

I read on these forums that its best to use preg_replace and setup an array for all the HTML markups I need replaced. For the life of me I can't understand how to use preg_replace. Would someone please give me some advise what is the best way to replace markups such as <strong> and </strong> to bold when it exports, this would be very much appreciated.

Rizier123
  • 58,877
  • 16
  • 101
  • 156
Daniel Ellison
  • 1,339
  • 4
  • 27
  • 49
  • Do you need something like this: `$data = "testtest"; echo $data = preg_replace("//", '&B', $data);` ? – Rizier123 Dec 17 '14 at 21:39
  • That worked like a charm it replaced all strong markups with &B. I was expecting that PHPExcel would bold the text but it just added &B as text. Would you know why that would be? – Daniel Ellison Dec 17 '14 at 21:56
  • Sry don't know anything about phpexecel... I only know how to use preg_replace. What you have to use as a replacement i don't know – Rizier123 Dec 17 '14 at 21:58

1 Answers1

1

'&B' will only set bold text for printed page headers and footers, as described in the section of the documentation entitled Setting the print header and footer of a worksheet

Formatting the content of a cell is described in the section of the documentation entitled Formatting cells, something like:

$objPHPExcel->getActiveSheet()->getCell('A1')->setValue('bold');
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);

or (if your content contains actual markup) using Rich Text objects if you only want to set part of the text to bold.

$objRichText = new PHPExcel_RichText();
$objRichText->createText('This text is ');

$objBoldTextRun = $objRichText->createTextRun('bold');
$objBoldTextRun->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getCell('B1')->setValue($objRichText);

but you will need to parse your markup to convert it to a Rich Text object

Mark Baker
  • 209,507
  • 32
  • 346
  • 385