phpword试用说明,php解析word内容,php读取word内容,php获取word内容方法
namespace app\controller;
use app\BaseController;
class Index extends BaseController
{
public function index(){
echo '<form action="'.url("wordtohtml").'" enctype="multipart/form-data" method="post"><input type="file" name="file" /> <br><input type="submit" value="上传" /> </form> ';
}
public function wordtohtml(){
$file = request()->file('file');
$ext_name = strtolower(pathinfo($file->getOriginalName(), PATHINFO_EXTENSION));
if($ext_name != 'docx'){
exit(result('文件格式不支持,请上传docx格式word文件',401,'校验失败'));
}
if($file-&gt;getSize() &gt; 2097152) {
exit(result('文件太大了,请上传小于2MB的word文件',401,'校验失败'));
}
$savename = \think\facade\Filesystem::putFile( 'uploads', $file);
$html = $this-&gt;wordParsing($savename);
//print_r();
unlink($savename);
}
//解析word内容并返回html
public function wordParsing($source)
{
//加载word文件 并 通过getSections获取word文档的全部元素
$sections = \PhpOffice\PhpWord\IOFactory::load($source)->getSections();
//定义html变量用于存储word文本内容
$html = '';
//循环所有元素
foreach($sections as $section) {
//获取当前元素的所有子元素
$elements = $section-&gt;getElements();
//循环当前子元素
foreach ($elements as $eky =&gt; $evl) {
$html .= '&lt;p&gt;';
if ($evl instanceof \PhpOffice\PhpWord\Element\TextRun) { //判断是否普通文本
$content_elements = $evl-&gt;getElements();
foreach ($content_elements as $eky2 =&gt; $evl2) {
$html .= $this-&gt;elementHandler($evl2, $evl);
}
}elseif($evl instanceof \PhpOffice\PhpWord\Element\PreserveText){ //判断是否保留元素(如自动生成链接的网址元素)
$data = $evl-&gt;getText();
$find = array('{','HYPERLINK','}',' ','&quot;','f','g');
$replace = '';
$resText = str_replace($find, $replace, $data);
if(isset($resText)){
$html .= $resText[0];
}
}elseif($evl instanceof \PhpOffice\PhpWord\Element\Table){
$all_table_elements = $evl-&gt;getRows();
$html .= '&lt;table style=&quot;margin:0;padding:0;border-collapse:collapse;border-spacing:0;&quot; &gt;';
foreach ($all_table_elements as $tky =&gt; $tvl) {
$html .= '&lt;tr style=&quot;padding:0&quot;&gt;';
$all_table_cells = $tvl-&gt;getCells();
foreach ($all_table_cells as $cky =&gt; $cvl){
$cell_elements = $cvl-&gt;getElements();
//获取表格宽度(返回单位为:缇)
$td_width = $cvl-&gt;getWidth();
$td_width_px = round($cvl-&gt;getWidth()/15,0);
$html .= '&lt;td style=&quot;border: 1px solid #777777;padding:2px 5px;width:'.$td_width_px.'&quot;&gt;';
foreach ($cell_elements as $cl){
//判断当存在elements属性时执行
if( property_exists($cl, 'elements') ){
$content_elements = $cl-&gt;getElements();
foreach ($content_elements as $eky2 =&gt; $evl2) {
$html .= $this-&gt;elementHandler($evl2, $cl);
}
}
}
$html .= '&lt;/td&gt;';
}
$html .= '&lt;/tr&gt;';
}
$html .= '&lt;/table&gt;';
}
$html .= '&lt;/p&gt;';
}
return $html;
}
}
//元素内容数据处理,$end_element最末级元素,是$parent_element的子元素;$parent_element为当前元素
public function elementHandler($end_element, $parent_element)
{
$html = '';
if ($end_element instanceof \PhpOffice\PhpWord\Element\Text) { //判断是否普通文本
$style = $end_element-&gt;getFontStyle();
//$fontFamily = mb_convert_encoding($style-&gt;getName(), 'GBK', 'UTF-8');
$fontFamily = $style-&gt;getName();
$fontSize = $style-&gt;getSize() ? ($style-&gt;getSize()/72)*96 : '';
$isBold = $style-&gt;isBold();
$fontcolor = $style-&gt;getColor();
$styleString = '';
$fontFamily &amp;&amp; $styleString .= &quot;font-family:{$fontFamily};&quot;;
$fontSize &amp;&amp; $styleString .= &quot;font-size:{$fontSize}px;&quot;;
$isBold &amp;&amp; $styleString .= &quot;font-weight:bold;&quot;;
$fontcolor &amp;&amp; $styleString .= &quot;color:{$fontcolor};&quot;;
$html .= sprintf('&lt;span style=&quot;%s&quot;&gt;%s&lt;/span&gt;',
$styleString,$end_element-&gt;getText()
//mb_convert_encoding($evl2-&gt;getText(), 'GBK', 'UTF-8')
);//dump($end_element-&gt;getText());
}elseif ($end_element instanceof \PhpOffice\PhpWord\Element\Link) { //判断是否链接
$style = $end_element-&gt;getFontStyle();
//$fontFamily = mb_convert_encoding($style-&gt;getName(), 'GBK', 'UTF-8');
$fontFamily = $style-&gt;getName();
$fontSize = $style-&gt;getSize() ? ($style-&gt;getSize()/72)*96 : '';
$isBold = $style-&gt;isBold();
$fontcolor = $style-&gt;getColor();
$styleString = '';
$fontFamily &amp;&amp; $styleString .= &quot;font-family:{$fontFamily};&quot;;
$fontSize &amp;&amp; $styleString .= &quot;font-size:{$fontSize}px;&quot;;
$isBold &amp;&amp; $styleString .= &quot;font-weight:bold;&quot;;
$fontcolor &amp;&amp; $styleString .= &quot;color:{$fontcolor};&quot;;
$html .= sprintf('&lt;a href=&quot;%s&quot; style=&quot;%s&quot;&gt;%s&lt;/a&gt;',
$end_element-&gt;getSource(), $styleString, $end_element-&gt;getText()
//mb_convert_encoding($evl2-&gt;getText(), 'GBK', 'UTF-8')
);
} elseif ($end_element instanceof \PhpOffice\PhpWord\Element\Image) { //判断是否图片
//可以在这里执行自定义方法将图片上传到OSS或者图片服务器哈
$imageDataTmp = $end_element-&gt;getImageStringData(true);
$imageType = $end_element-&gt;getImageType() ? $end_element-&gt;getImageType() : 'image/jpg';
$imageData = 'data:' . $imageType . ';base64,' . str_replace(array(&quot;\r\n&quot;, &quot;\r&quot;, &quot;\n&quot;), &quot;&quot;, $imageDataTmp);
//保存文件
//$imageSrc = './uploads/' . md5($end_element-&gt;getSource()) . '.' . $end_element-&gt;getImageExtension();
//file_put_contents($imageSrc,base64_decode(explode(',',$imageData)[1]));
$html .= '&lt;img src=&quot;'. $imageData .'&quot; style=&quot;width:100%;height:auto&quot;&gt;';
}
return $html;
}
}
**PS:**文件为测试试用,有点乱,可以自行整理哈。