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.

3
  • Do you need something like this: $data = "<strong>testtest<strong><strong>"; echo $data = preg_replace("/<strong>/", '&B', $data); ? Commented Dec 17, 2014 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? Commented Dec 17, 2014 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 Commented Dec 17, 2014 at 21:58

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.