安静
PHP技术博客

140312 discuz帖子内容还原HTML

最近挖了个坑把自己埋得差不多了,唉,说点什么好呢?太lower么

新功能需要还原贴内信息,同步到其他系统。网上看了下并没有相关内容,并且有不少遇到了parseattach还原问题。这里发下我解决的代码:

关于贴内附件还原discuz原始方法:

$tid=181281;
$pid=4645197;
$aid=190137;
$pids = array (
$pid 
);
$attachs = array (
$pid => array (
$aid 
) 
);
$list = array (
$pid => array (
'message' => '嘀嗒嘀嗒嘀嗒嘀嗒嘀嗒嘀嗒的 的的的 的 <br />[attach]190137[/attach]<br />',
'pid' => $pid,
'attachments' => array () 
) 
);
require_once libfile ( 'function/attachment' );
$_G ['tid'] = $tid;
parseattach ( $pids, $attachs, $list );
var_dump($list);

改良过得帖子内容还原

/**
*@param: 处理discuz帖子还原相关代码demo
*@desc : 2个函数去作为获取附件并且返回附件url参数最后替换attach
*/
$thread_info = DB::fetch_first ( "select a.`tid`, a.`authorid`, a.`author`,a.`dateline`, a.`subject`, b.`message` from " . DB::table ( 'forum_thread' ) . " as a, " . DB::table ( 'forum_post' ) . " as b where a.tid=$tid and a.tid=b.tid and b.first=1 order by pid desc limit 1" );

require_once libfile ( 'function/discuzcode' );

$thread_info ['dateline'] = date ( "Y-m-d H:i:s", $thread_info ['dateline'] );
$thread_info ['message'] = discuzcode ( $thread_info ['message'] );

if (preg_match_all ( "/\[attach\](\d+)\[\/attach\]/i", $thread_info ['message'], $matchaids )) {
$attach_ids = $matchaids [1];
}
$attach_list = array ();
foreach ( $attach_ids as $aid ) {
$find = "/\[attach\]$aid\[\/attach\]/i";
$thread_info ['message'] = preg_replace ( $find, get_lw_attach_path ( $aid ), $thread_info ['message'], 1 );
$thread_info ['message'] = preg_replace ( $find, '', $thread_info ['message'] );

}



function get_lw_attach_path($aid) {
global $_G;
$return = $filename = '';
if ($attach = C::t ( 'forum_attachment_n' )->fetch ( 'aid:' . $aid, $aid, array (1,- 1) )) {
return get_lw_attach_path_str ( $attach );
}
return $filename;
}
function get_lw_attach_path_str($attach) {
global $_G;

if (! $attach ['isimage']) {
return '<a href="' . $_G ['siteurl'] . 'forum.php?mod=attachment&aid=' . aidencode ( $attach ['aid'] ) . '">' . $attach ['filename'] . '</a>';
}
if ($attach ['remote']) {
$imgurl = $_G ['setting'] ['ftp'] ['attachurl'] . 'forum/' . $attach ['attachment'];

return '<p><img onclick="viewimage(this);" src="' . $imgurl . '" /></p>';
} else {
if (preg_match ( '/^(?!http:)/', $attach ['url'] )) {
$attach ['url'] = $_G ['siteurl'] . 'data/attachment/forum/' . $attach ['url'];
}
$imgurl = $attach ['url'] . $attach ['attachment'] . ($_G ['gp_width'] ? '&width=' . $_G ['gp_width'] : '') . ($_G ['gp_height'] ? '&height=' . $_G ['gp_height'] : '');

return '<p><a href="' . $imgurl . '" target="_blank"><img height="320" width="320" src="' . $imgurl . '" /></a></p>';
}

}
赞(0) 打赏
未经允许不得转载:AJ's Blog » 140312 discuz帖子内容还原HTML
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏