Asked  7 Months ago    Answers:  5   Viewed   182 times

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.



'&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:


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

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

Saturday, May 29, 2021
answered 7 Months ago

Eventually solved it with the help of TiMESPLiNTER (give him the credit!), didn't know $1 didn't get brought along and that you have to call a variable $matches in the callback function and get $matches[1] instead of $1.

$counter = 1;

function parse(){
    function cb($matches){
        global $counter;

        return '<span class="b">'. $this->counter .'. ' . $matches[1] . '</span>';

    $string = preg_replace_callback("_(.*?)_si", 'cb', $string);
Saturday, May 29, 2021
answered 7 Months ago

EDIT I added a pull request with a fix:

This happens when you try to duplicate cell's style to the same cell; Take a look at this:

$phpe = new PHPExcel();
$sheet = $phpe->createSheet();

$sheet->setCellValue('A1', 'hi there') ;
$sheet->setCellValue('A2', 'hi again') ;

$sheet->duplicateStyle($sheet->getStyle('A1'), 'A2');

$writer = new PHPExcel_Writer_Excel2007($phpe);

It will work just fine. BUT if I add another line like this:

$sheet->duplicateStyle($sheet->getStyle('A1'), 'A1');

then bang, infinite recursion starts after calling the save method

To fix your code, you should modify this part:

$sheet->duplicateStyle((($level == 0) ?  $this->styles['category'] : $this->styles['subcategory']), Cell::NUMBER . $row);

To something along the lines of:

$style = ($level == 0) ?  $this->styles['category'] : $this->styles['subcategory'];
$targetCoords = Cell::NUMBER . $row;
if($style->getActiveCell() != $targetCoords) {
    $sheet->duplicateStyle($style, $targetCoords);
Saturday, May 29, 2021
answered 7 Months ago

It's mentioned in the documentation.

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

    $objPayable = $objRichText->createTextRun('payable within thirty days after the end of the month');
    $objPayable->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) );

    $objRichText->createText(', unless specified otherwise on the invoice.');


Saturday, May 29, 2021
answered 7 Months ago

Android >= Honeycomb (self-explaining)


Android < Honeycomb Instead of directly setting it to the position you will tell "set it x pixels away from the left/top/right/bottom border".

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.setMargins(x, y, 0, 0);

Saturday, October 30, 2021
Manish Das
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :