问题
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.
回答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
来源:https://stackoverflow.com/questions/27535251/phpexcel-how-to-replace-text-using-preg-replace