PHPExcel - 如何使用preg_replace替换文本 [英] PHPExcel - How to replace text using preg_replace

查看:165
本文介绍了PHPExcel - 如何使用preg_replace替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHPExcel将数据库中的数据提取到有组织的Excel工作表上。除了一件事外,一切都很好。我的数据库条目有时可能包含HTML标记,如< strong>< / strong> < BR> < p>< / p> etc ...所以我设法让这条PHP线路工作,这很好地取代我的< ; BR> 标记到空格。

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

但是,当我尝试执行以下操作时,它什么也不做。我期待着它会大胆的文字。

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

我在这些论坛上看到,最好使用preg_replace并为所有HTML标记设置数组I需要更换。对于我的生活,我无法理解如何使用preg_replace。有人请给我一些建议什么是取代< strong> < / strong> 以粗体输出时,会非常感激。

页面页眉和页脚,如标题为设置工作表的打印页眉和页脚的文档部分所述



格式化单元格的内容在标题为格式化单元格的文档中描述,例如:

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

或者(如果您的内容包含实际的标记)使用富文本对象,如果您只想设置文本加粗。

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

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

但您需要解析标记以将其转换为Rich Text对象


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:

$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

这篇关于PHPExcel - 如何使用preg_replace替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆