获取内容中图片URL

Discuz!X贴子生成文章后,列表页的图片能自动显示出来吗?
为解决这一问题,故有下文。(由supesite转换到discuz!x文章的之前有解决过。不清楚的同学请搜索。)

先简单的说一下由贴子生成文章后,内容中图片的标签是:

 [attach]123[/attach]

没错,你真厉害,一眼就看出这是discuz常用的附件图标签。

接下来请看解决方法,确保无例外情况发生,增加了直接获取img标签的图片URL。(方法不错!!!)

获取贴子附件图标签ID转为图片URL

//获取内容中图片URL
function get_picture($message) {
$pic = '';
$message = stripslashes($message);
$message = preg_replace("/\<img src=\".*?image\/smiley\/(.+?).gif\".*?\>\s*/is", '', $message);    //移除表情符
preg_match("/src\=[\"\']*([^\>\s]{25,105})\.(jpg|gif|png)/i", $message, $mathes);
if(!empty($mathes[1]) || !empty($mathes[2]))  $pic = "{$mathes[1]}.{$mathes[2]}";
return addslashes($pic);
}

if($value['pic']) {
$value['pic'] = pic_get($value['pic'], 'portal', $value['thumb'], $value['remote']);
}else{
$match = $matchaids = $attachtags = $firstpost = array();
$firstpost['message'] = DB::result(DB::query("SELECT content FROM ".DB::table('portal_article_content')." WHERE aid='$value[aid]'"), 0);
if($_G['group']['allowgetattach']) {
if(preg_match_all("/\[attach\](\d+)\[\/attach\]/i", $firstpost['message'], $matchaids)) {
$attachtags = $matchaids[1];
}
}

if($attachtags[0]){
//获取贴子附件图标签ID转为图片URL [attach]123[/attach]
$query2 = DB::query("SELECT * FROM ".DB::table('forum_attachment')." WHERE aid='$attachtags[0]'");
while ($value2 = DB::fetch($query2)) {
$match = $value2;
}

//print_r($match);
if($attachtags[0]) $value['pic'] = pic_get($match['attachment'], 'forum', $match['thumb'], $match['remote']);
}else{
//获取内容中图片URL <img src="imageurl"></img>
if($attachtags[0]) $value['pic'] = get_picture($firstpost['message']);
}
}

相关文章:

1、正则(pcre.backtrack_limit)最大回溯/递归限制
https://www.onexin.net/regular-pcre-backtrack_limit-maximum-backtracking-recursion-limits/

2、php正则过滤html标签、空格、换行符
https://www.onexin.net/html-tags-php-regular-filter-space-line-breaks/

3、preg_replace与str_replace函数的比较
https://www.onexin.net/preg_replace_str_replace/

转载请注明出处:https://www.onexin.net/image-url-for-content/

One Reply to “获取内容中图片URL”

Leave a Reply

Your email address will not be published. Required fields are marked *