-
-
/** - * 导出word格式文档
- * 只添加一次关键词链接
- * edit bbs.it-home.org
- */
- //var_dump($_SERVER["HTTPS"]);die;
- class word
- {
- function start()
- {
- ob_start();
- echo ‘xmlns:w=”urn:schemas-microsoft-com:office:word”
- xmlns=”http://www.w3.org/TR/REC-html40″>’;
- }
function save($path)
- {
- print “”;
- $data = ob_get_contents();
- ob_end_clean();
- $this->wirtefile ($path,$data);
- }
function wirtefile ($fn,$data)
- {
- $fp=fopen($fn,”wb”);
- fwrite($fp,$data);
- fclose($fp);
- }
- }
- $db = mysql_connect(‘localhost’, ‘root’, ’123123′) or die(“Could not connect to database.”);//连接数据库
mysql_select_db(‘test’); //选择数据库
- $sql = “select info.itemid,info.title,info.tag,con.content from csign_info_22 as info left join csign_info_data_22 as con on con.itemid = info.itemid”;
mysql_query(‘set names “utf8″‘);
- $res=mysql_query($sql, $db);
while($info=mysql_fetch_array($res))
- {
- if($info['tag']){
- $tagexp = explode(‘ ‘,$info['tag']);
- foreach($tagexp as $k=>$v){
- $tagstr .= ‘’.$v.’ ’;
if(strpos($info['content'],$v)){
- $info['content'] =str_replace_once($v,””.$v.””,$info['content']);
- }
- }
- }
- //echo $info['content'];die;
- $html = ‘
’.$info['title'].’ ’.$info['content'].’ ’;
if($tagstr){$html.= ‘ 关键词: ’.$tagstr.’ ’;}
- $html .= ‘
本文由程序员之家收集整理,更多信息请访问Tiandone ’;
- $word = new word();
- $word->start();
- //$html = “aaa”.$i;
- $title = iconv(“UTF-8″,”GB2312″,$info['title']);
- //echo $title;die;
- $wordname = ‘E:/web/paypal/product/tiandone/’.$title.”.doc”;//存放目录
- //echo $wordname;die;
- echo $html;
- $word->save($wordname);
- ob_flush();//每次执行前刷新缓存
- flush();
- //print_r($info);die;
- //die;
- }
- //只匹配一次关键词 重复的不匹配
- function str_replace_once($needle, $replace, $haystack) {
- // Looks for the first occurence of $needle in $haystack
- // and replaces IT with $replace.
- $pos = strpos($haystack, $needle);
- if ($pos === false) {
- // Nothing found
- return $haystack;
- }
- return substr_replace($haystack, $replace, $pos, strlen($needle));
- }
- ?>
-
-
复制代码
以上就是今天php教程给出的代码,供大家学习研究之用。
学php就来程序员之家吧。
|