当前位置:首页 » 编程语言 » php处理word

php处理word

发布时间: 2023-01-13 11:01:19

php读取word文档怎么处理乱码

通过com调用word组件,试试。

<?
// this script is come from zend. :)
$word = new COM( "word.application ") or die( "Unable to instanciate Word ");
print "Loaded Word, version {$word-> Version}\n ";
$word-> Visible = 1;
$word-> Documents-> Add();
$word-> Selection-> TypeText( "This is a test... ");
$word-> Documents[1]-> SaveAs( "Useless test.doc ");
$word-> Quit();
?>

Ⅱ php word转pdf 有什么方法吗

PHP也可以实现导出Word文档为PDF的功能,不过要借助于第三方的类库,今天我们将为大家介绍PHP依靠com.sun.star.ServiceManager来转换Word为PDF文档的相关技巧。

PHP处理Word转PDF的示例代码:

02set_time_limit(0);
03functionMakePropertyValue($name,$value,$osm){
04$oStruct=$osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue");
05$oStruct->Name=$name;
06$oStruct->Value=$value;
07return$oStruct;
08}
09functionword2pdf($doc_url,$output_url){
10$osm=newCOM("com.sun.star.ServiceManager")ordie("请确认OpenOffice.org库是否已经安装. ");
11$args=array(MakePropertyValue("Hidden",true,$osm));
12$oDesktop=$osm->createInstance("com.sun.star.frame.Desktop");
13$oWriterDoc=$oDesktop->loadComponentFromURL($doc_url,"_blank",0,$args);
14$export_args=array(MakePropertyValue("FilterName","writer_pdf_Export",$osm));
15$oWriterDoc->storeToURL($output_url,$export_args);
16$oWriterDoc->close(true);
17}
18$output_dir="D:/temp/";
19$doc_file="D:/temps/test.doc";
20$pdf_file="test.pdf";
21$output_file=$output_dir.$pdf_file;
22$doc_file="file:///".$doc_file;
23$output_file="file:///".$output_file;
24word2pdf($doc_file,$output_file);
25?>

Ⅲ PHP怎么样去掉从word直接粘贴过来的没有用的

一般处理的方式有二种:

  1. 通过编辑器的JS直接去除。

2.提交到后台后,直接用程序去掉无效标签。下面我就分享一个通过PHP的处理方式,成功率可能不是100%。这程序也是在PHP官网上看到的,就顺便粘贴过来了。
复制代码 代码如下:
function ClearHtml($content,$allowtags='') {

mb_regex_encoding('UTF-8');
//replace MS special characters first
$search = array('/‘/u', '/’/u', '/“/u', '/”/u', '/—/u');
$replace = array(''', ''', '"', '"', '-');
$content = preg_replace($search, $replace, $content);
//make sure _all_ html entities are converted to the plain ascii equivalents - it appears
//in some MS headers, some html entities are encoded and some aren't
$content = html_entity_decode($content, ENT_QUOTES, 'UTF-8');
//try to strip out any C style comments first, since these, embedded in html comments, seem to
//prevent strip_tags from removing html comments (MS Word introced combination)
if(mb_stripos($content, '/*') !== FALSE){
$content = mb_eregi_replace('#/*.*?*/#s', '', $content, 'm');
}
//introce a space into any arithmetic expressions that could be caught by strip_tags so that they won't be
//'<1' becomes '< 1'(note: somewhat application specific)
$content = preg_replace(array('/<([0-9]+)/'), array('< $1'), $content);

$content = strip_tags($content, $allowtags);
//eliminate extraneous whitespace from start and end of line, or anywhere there are two or more spaces, convert it to one
$content = preg_replace(array('/^ss+/', '/ss+$/', '/ss+/u'), array('', '', ' '), $content);
//strip out inline css and simplify style tags
$search = array('#<(strong|b)[^>]*>(.*?)</(strong|b)>#isu', '#<(em|i)[^>]*>(.*?)</(em|i)>#isu', '#<u[^>]*>(.*?)</u>#isu');
$replace = array('<b>$2</b>', '<i>$2</i>', '<u>$1</u>');
$content = preg_replace($search, $replace, $content);

//on some of the ?newer MS Word exports, where you get conditionals of the form 'if gte mso 9', etc., it appears
//that whatever is in one of the html comments prevents strip_tags from eradicating the html comment that contains
//some MS Style Definitions - this last bit gets rid of any leftover comments */
$num_matches = preg_match_all("/<!--/u", $content, $matches);
if($num_matches){
$content = preg_replace('/<!--(.)*-->/isu', '', $content);
}
return $content;
}

测试使用结果:
复制代码 代码如下:
<?php
$content = ' <!--[if gte mso 9]><xml><w:WordDocument><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><w:>0</w:><w:>2</w:><w:DocumentKind>DocumentNotSpecified</w:DocumentKind><w:DrawingGridVerticalSpacing>7.8</w:DrawingGridVerticalSpacing><w:View>Normal</w:View><w:Compatibility></w:Compatibility><w:Zoom>0</w:Zoom></w:WordDocument></xml><![endif]-->
<p style="text-indent: 24.0000pt; margin-bottom: 0pt; margin-top: 0pt;"><span style="mso-spacerun: "yes"; font-size: 12.0000pt; font-family: "宋体";">《优伴户外旅行》——让旅行成为习惯!</span></p>越发忙碌的你,是否想给自己放个假?专注工作的你,是否还记得上一次锻炼是什么时候?优伴户外旅行,给你不一样的旅行体验:给心自由,便处处都是风景!</span></p>';
echo ClearHtml($content,'<p>');

/*
得到的结果:
<p >《优伴户外旅行》--让旅行成为习惯!</p>越发忙碌的你,是否想给自己放个假?专注工作的你,是否还记得上一次锻炼是什么时候?优伴户外旅行,给你不一样的旅行体验:给心自由,便处处都是风景!</p>
*/
?>

Ⅳ 怎样用PHP读取一个word文档内容,并且包含样式入库

可以试下phpoffice/phpword库。

phpoffice包含了office套件中的word,excel, powerpoint,visio等比较常用的软件文档的处理库。可以用于读取及生成相应的文档。

具体操作方法可以查看官方文档,以及示例代码。

PHPOffice/PHPWord

Ⅳ php操作word文件,求方法谢谢。

如果你本地没有安装 office ,那么系统里是没有 com 可供调用的(也就是说,使用new com的方法不一定行得通)。

如果你想要生成 word 文件给别人用,可以先生成 mht 文件(就是将 html 及相关资源打包),然后发送给客户端。相关编码可参考如下资料:

http://www.cnblogs.com/phphuaibei/archive/2011/11/30/2269427.html

Ⅵ 怎样用PHP读取一个word文档内容并在浏览器中显示出来

目前程序编译语言有很多种,其中php是最为常见的一种编程语言。php读取word文档是很多朋友都想了解的,下面就由达内的老师为大家介绍一下。
?php
/*
*
必须将
php.ini
中的
com.allow_dcom
设为
TRUE
*/
function
php_Word($wordname,$htmlname,$content)
{
//获取链接地址
$url
=
$_SERVER['HTTP_HOST'];
$url
=
";
$url
=
$url.$_SERVER['PHP_SELF'];
$url
=
dirname($url)."/";
//建立一个指向新COM组件的索引
$word
=
new
COM("word.application")
or
die("Unable
to
instanciate
Word");
//显示目前正在使用的Word的版本号
echo
"Loading
Word,
v.
{$word-
Version}";
//把它的可见性设置为0(假),如果要使它在最前端打开,使用1(真)
$word->Visible
=
1;
//---------------------------------读取Word内容操作
START-----------------------------------------
//打开一个word文档
$word->Documents->Open($url.$wordname);
//将filename.doc转换为html格式,并保存为html文件
$word->Documents[1]->SaveAs(dirname(__FILE__)."/".$htmlname,8);
//获取htm文件内容并输出到页面
(文本的样式不会丢失)
$content
=
file_get_contents($url.$htmlname);
echo
$content;
//获取word文档内容并输出到页面(文本的原样式已丢失)
$content=
$word->ActiveDocument->content->Text;
echo
$content;
//关闭与COM组件之间的连接
$word->Documents->close(true);
$word->Quit();
$word
=
null;
unset($word);
//---------------------------------新建立Word文档操作
START--------------------------------------
//建立一个空的word文档
$word->Documents->Add();
//写入内容到新建word
$word->Selection->TypeText("$content");
//保存新建的word文档
$word->Documents[1]->SaveAs(dirname(__FILE__)."/".$wordname);
//关闭与COM组件之间的连接
$word->Quit();
}
php_Word("tesw.doc","filename.html","写入word的内容");
?>

Ⅶ 用php 读取word 文档内容 比如:word文档为试题等等

这个是通过调用com组件的方式操作word的

<?
// 建立一个指向新COM组件的索引
$word = new COM("word.application") or die("Can't start Word!");
// 显示目前正在使用的Word的版本号
//echo “Loading Word, v. {$word->Version}<br>”;
// 把它的可见性设置为0(假),如果要使它在最前端打开,使用1(真)
// to open the application in the forefront, use 1 (true)
//$word->Visible = 0;
//打?一个文档
$word->Documents->OPen("d:\myweb\muban.doc");
//读取文档内容
$test= $word->ActiveDocument->content->Text;
echo $test;
echo "<br>";
//将文档中需要换的变量更换一下
$test=str_replace("<{变量}>","这是变量",$test);
echo $test;
$word->Documents->Add();
// 在新文档中添加文字
$word->Selection->TypeText("$test");
//把文档保存在目录中
$word->Documents[1]->SaveAs("d:/myweb/comtest.doc");
// 关闭与COM组件之间的连接
$word->Quit();
?>

Ⅷ php5.3怎么将word解码

echo base64_encode(file_get_contents('word文档'));

//编码
echo base64_ decode(file_get_contents('word文档'));

//解码
有一个扩展好像可以处理word文档,好像是COM的可以去看看

Ⅸ 如何使用PHP显示在线Word文档

php 在线预览 word 文档,有多种方法,本质上来说,大多是调用客户端电脑系统安装的 Word 软件。可以用 php 语言的 com 支持功能函数来完成,比如:

$word = new COM("word.application") or die("Unable to instanciate Word");$word->Documents->Open($url.$wordname);

或者使用 word 文档的插件来完成,比如:swftools 等。

热点内容
python按任意键 发布:2025-07-14 09:49:19 浏览:291
内置存储交换位置 发布:2025-07-14 09:33:10 浏览:647
甲壳虫密码如何解锁 发布:2025-07-14 09:23:55 浏览:823
解压专家解压迅雷云盘 发布:2025-07-14 09:11:09 浏览:910
编程基础教学 发布:2025-07-14 09:09:48 浏览:94
电脑板能进入的手机版服务器 发布:2025-07-14 09:08:10 浏览:555
roblox电脑版服务器推荐 发布:2025-07-14 09:06:07 浏览:807
application缓存 发布:2025-07-14 09:01:56 浏览:345
安卓怎么看绑定地区 发布:2025-07-14 09:01:49 浏览:85
籽岷的生存服务器IP 发布:2025-07-14 08:51:06 浏览:419