智睿软件_技术交流论坛综合站长Web程序开发 → asp如何提取文章中的图片信息


  共有21487人关注过本帖树形打印

主题:asp如何提取文章中的图片信息

帅哥哟,离线,有人找我吗?
zhirui
  1楼 个性首页 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:管理员 帖子:520 积分:8740 威望:0 精华:5 注册:2009-07-07
asp如何提取文章中的图片信息  发帖心情 Post By:2012-09-29 11:13:48

<%
Str = "<img src='图片地址' alt='3.jpg' /> <img alt=说明 src='1.jpg'/> <img src='2.jpg'/>"
Set regEx = New RegExp '建立正则表达式。
regEx.Pattern = "(<img)(.[^<>]*)(src=)('|"&CHR(34)&"| )?(.[^'|\s|"&CHR(34)&"]*)(\.)(jpg|gif|png|bmp|jpeg)('|"&CHR(34)&"|\s|>)(.[^>]*)(>)" '设置模式。
regEx.IgnoreCase = True '设置是否区分字符大小写。
regEx.Global = True '设置全局可用性。
Set Matches = regEx.Execute(Str) '执行搜索。

For Each Match in Matches '遍历匹配集合。
'输入图片地址
Response.Write Match.SubMatches(4)&"."&Match.SubMatches(6)&"<br>"
Next
%>比如我做一个新闻发布系统,首页有图片新闻,我想从发布的文章中提取出第一张图片,然后在首页显示。
请高手指导。。。
问题补充:我自己写了一个,大家帮忙找下不足哈。
<script language="javascript">
var pic;
var str = 'e,qwe,qwe<img alt=sdfsf src="asdasdasd.jpg" />qwe,q,we';
var newarray = new Array();
newarray = str.split("<img");
newarray = newarray[1].split("src=");
newarray = newarray[1].split("/>");
newarray = newarray[0].split(" ");
pic = newarray[0];
pic = pic.replace("\"","").replace("\"","");
pic = pic.replace("\'","").replace("\'","");
document.write (pic);
</script>

答案
<iframe id="editor" width="90%" height="100"></iframe> <script>editor.document.designMode="on";</script><input type="button" value="获取图片" />
<script type="text/Javascript">
<!--
function getImg()
{
var str_img = editor.document.body.innerHTML.replace(/[\s\S]*?src=[\'\"\s]*?(http:\/\/[a-z\d\._\-\/\%]*)[\'\"\s]*?/igm, '$1,');
var imgs = str_img.split(',');
str_img = '';
for (var i=0;i<imgs.length-1;i++)
{
if (imgs[i].indexOf('http://')==0)
{
if (i>0)str_img += ',';
str_img += imgs[i];
}
}
editor.document.body.innerHTML=(str_img);
}
//-->
</script>

-

保存图片的SRC地址

asp+access+sql   2007-11-18 19:48    阅读3    评论0 字号: 大大 中中 小小
方法1
content=sContent1
regstr="src\=.+?\.(gif|jpg)"
url=Replace(Replace(Replace(RegExp_Execute(regstr?content)?"'"?"")?""""?"")?"src="?"")
Response.Write(url)
Response.end

'返回匹配值
Function RegExp_Execute(patrn? strng)
Dim regEx? Match? Matches?values '建立变量。
Set regEx = New RegExp '建立正则表达式。
regEx.Pattern = patrn '设置模式。
regEx.IgnoreCase = true '设置是否区分字符大小写。
regEx.Global = True '设置全局可用性。
Set Matches = regEx.Execute(strng) '执行搜索。
For Each Match in Matches '遍历匹配集合。
values=values&Match.Value&"?"
Next
RegExp_Execute = values
End Function

 

方法2


以下为代码部分:
C

response.write savepic(Content)
response.end()

function savepic(str)
content=str&""
regstr="src\=.+?\.(gif|jpg)"
url=Replace(Replace(Replace(RegExp_Execute(regstr?content)?"'"?"")?""""?"")?"src="?"")
savepic=url
end function
Function RegExp_Execute(patrn? strng)
Dim regEx? Match? Matches?values '建立变量。
Set regEx = New RegExp '建立正则表达式。
regEx.Pattern = patrn '设置模式。
regEx.IgnoreCase = true '设置是否区分字符大小写。
regEx.Global = True '设置全局可用性。
Set Matches = regEx.Execute(strng) '执行搜索。
For Each Match in Matches '遍历匹配集合。
values=values&Match.Value&"?"
Next
RegExp_Execute = values
End Function

方法3

以下为代码部分:
http://[a-z0-9A-Z.?/_]*.(jpg|jpeg|gif|png)

php方法

今天继续编写新网站下的文章发布系统,其中需要一个功能,就是显示焦点新闻中的图片,本来可以在录入时单独上传一个与新闻搭配的图片,这是最简单的,但为了给自己一个挑战,我打算做成录入带有图片的新闻后,程序会自动提取正文中的第一个图片的地址,鼓捣了半天,成果如下:

$p = '/]+)src="([^"]+)"([^\/>]+)\/>/is';
$f = preg_match($p?$c?$re);
echo $re[2];

此需求的难点在于,通过FCKeditor添加的图片中,其alt属性的位置是不固定的:


以下为代码部分:
<img src="url" alt="" />或<img alt="" src="url" />


因此,正则表达式必须完全兼容这2种情况


【转载】http://www.cnblogs.com/sbdx/archive/2006/11/04/550213.html

<%
'str="<a href=""post.php?action=newthread&amp;fid=2&amp;extra=page%3D1""><img alt='123' src=""http://127.0.0.1/images/blue/newtopic.gif"" border=""0"" alt="""" /></a><img src=""/customavatars/user.gif"" vspace=""5""><br /><img src=""/customavatars/user.gif"" vspace=""5""><img src=""images/blue/top.gif"" border=""0"" alt=""顶部"" />"
str="<img src=images/200610261533.jpg><img src=""images/200609151656.jpg""><img src=images/200609051735.JPG>"

response.write(GetImgSrc(str))

function GetImgSrc(str) '取得img 标签内容
    dim tmp
    Set objRegExp = New Regexp
     objRegExp.IgnoreCase = True    '忽略大小写
     objRegExp.Global = false        '全文搜索 !关键!
     objRegExp.Pattern = "<img (.*?)src=(.[^\[^>]*)(.*?)>"
    Set Matches =objRegExp.Execute(str)
    For Each Match in Matches
         tmp=tmp & Match.Value
    Next
     GetImgSrc=getimgs(tmp)
end function

function getimgs(str)'取得
    Set objRegExp1 = New Regexp
     objRegExp1.IgnoreCase = True    '忽略大小写
     objRegExp1.Global = True    '全文搜索
     objRegExp1.Pattern = "src=.+[(gif|jpg|png)]+"    '表达式
    set mm=objRegExp1.Execute(str)
    For Each Match1 in mm
         imgsrc=Match1.Value
        '也许存在不能过滤的字符,确保万一
         imgsrc=replace(imgsrc,"""","")
         imgsrc=replace(imgsrc,"src=","")
         imgsrc=replace(imgsrc,"<","")
         imgsrc=replace(imgsrc,">","")
         imgsrc=replace(imgsrc,"img","")
         imgsrc=replace(imgsrc," ","")
         getimgs=getimgs&imgsrc'把里面的地址串起来备用
    next
end function
%>

:<img src=image/ad1.gif width="128" height="36"/><img src='image/ad2.gif' width="128" height="36" />
正则表达式:<img[\s]+src[\s]*=[\s]*((['"](?<src>[^'"]*)[\'"])|(?<src>[^\s]*))
提取的结果:
image/ad1.gif
image/ad2.gif<iframe id="editor" width="90%" height="100"></iframe> <script>editor.document.designMode="on";</script><input type="button" value="获取图片" />
  <script type="text/javascript">
  <!--
        function getImg()
    {
        var str_img = editor.document.body.innerHTML.replace(/[\s\S]*?src=[\'\"\s]*?(http:\/\/[a-z\d\._\-\/\%]*)[\'\"\s]*?/igm, '$1,');
        var imgs = str_img.split(',');
        str_img = '';
        for (var i=0;i<imgs.length-1;i++)
        {
            if (imgs[i].indexOf('http://')==0)
            {
                if (i>0)str_img += ',';
                str_img += imgs[i];
            }
        }
        editor.document.body.innerHTML=(str_img);
    }
  //-->
  </script>


//******************
博客加了一个版面,浏览模式,需要出文章中提取一张图片做预览吧。图片可以过虑,文字就过虑不掉了。还不能全部显示,就显示一张,这搞的我的头大啊。

上百度,GOOGLE查了一上午的资料终于搞定。

下面是这个函数:

'==================================================
'过程名:GetImg
'作 用:取得文章中第一张图片
'参 数:str ------ 文章内容
'参 数:strpath ------ 保存图片的路径
'==================================================
Function GetImg(str,strpath)
set objregEx = new RegExp
objregEx.IgnoreCase = true
objregEx.Global = true
zzstr=""&strpath&"(.+?)\.(jpg|gif|png|bmp)"
objregEx.Pattern = zzstr
set matches = objregEx.execute(str)
for each match in matches
retstr = retstr &"|"& Match.Value
next
if retstr<>"" then
Imglist=split(retstr,"|")
Imgone=replace(Imglist(1),strpath,"")
GetImg=Imgone
else
GetImg=""
end if
end function

但是它只能取 后缀 不能取html里的<img>所以我们需要加上正侧


Function GetImg(str)
dim objregEx,zzstr,matches,Match,retstr,Imglist,Imgone,strpath
set objregEx = new RegExp
objregEx.IgnoreCase = true
objregEx.Global = true
zzstr="<img(.+?)\.(jpg|gif|png|bmp)(.+?)>"
objregEx.Pattern = zzstr
set matches = objregEx.execute(str)
for each match in matches
retstr = retstr &"|"& Match.Value
next
if retstr<>"" then
Imglist=split(retstr,"|")
Imgone=replace(Imglist(1),strpath,"")
GetImg=Imgone
else
GetImg=""
end if
end function

调用的时候 只需要 <%=GetImg(rs("字段"))%>

 


政府公安政府系统,专注于政府媒体,公安公众信息系统 支持(0中立(0反对(0回到顶部
帅哥哟,离线,有人找我吗?
zhirui
  2楼 个性首页 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:管理员 帖子:520 积分:8740 威望:0 精华:5 注册:2009-07-07
  发帖心情 Post By:2012-09-29 11:14:04

<!--#include file="Include/conn.asp"-->
<!--#include file="Include/Fun_SqlIn.Asp"-->

<%
function GetImgSrc(str) '取得img 标签内容
    dim tmp
    Set objRegExp = New Regexp
     objRegExp.IgnoreCase = True    '忽略大小写
     objRegExp.Global = false        '全文搜索 !关键!
     objRegExp.Pattern = "<img (.*?)src=(.[^\[^>]*)(.*?)>"
    Set Matches =objRegExp.Execute(str)
    For Each Match in Matches
         tmp=tmp & Match.Value
    Next
     GetImgSrc=getimgs(tmp)
end function

function getimgs(str)'取得
    Set objRegExp1 = New Regexp
     objRegExp1.IgnoreCase = True    '忽略大小写
     objRegExp1.Global = True    '全文搜索
     objRegExp1.Pattern = "src=.+[(gif|jpg|png)]+"    '表达式
    set mm=objRegExp1.Execute(str)
    For Each Match1 in mm
         imgsrc=Match1.Value
        '也许存在不能过滤的字符,确保万一
         imgsrc=replace(imgsrc,"""","")
         imgsrc=replace(imgsrc,"src=","")
         imgsrc=replace(imgsrc,"<","")
         imgsrc=replace(imgsrc,">","")
         imgsrc=replace(imgsrc,"img","")
         imgsrc=replace(imgsrc," ","")
         getimgs=getimgs&imgsrc'把里面的地址串起来备用
    next
end function

  dim rs,sql,i,strurl,regstr
  set rs = server.createobject("adodb.recordset")
  sql="select Top 10 * from zhi_rui_s_io where ViewFlag=1 and IndexFlag=1 order by IndexFlag desc,id desc"
  rs.open sql,conn,1,1
  response.write"<table width='100%' border='0' cellspacing='0' cellpadding='0' style='margin-top:10px;'>"
  if rs.bof and rs.eof then
    response.write "<tr><td align='center'>暂无相关信息</td></tr>"
  else
    response.write"<tr>"
    for i=1 to 1'rs.recordcount
      if rs.eof then exit for
   response.write"<td width='10%' >"
 'Response.Write rs("content")
'response.write"<img "&RegExp_Execute(regstr,strurl)&" style='border:1px solid #FFFFFF;' width=120 height=100  class='shadow'>"
   'response.write"<div ><a  href='info_Show.asp?InfoId="&rs("ID")&"&ClassId="&rs("ClassId")&"&Topid="&rs("TopID")&"' target='_blank'><img src='"&rs("MaxPic")&"' style='border:1px solid #FFFFFF;' width=120 height=100  class='shadow'></a></div>"
   response.write"<div  align='center'>"
   response.write""&left(rs("infoName"),10)&""
   response.write"</div>"
   response.write"</td>"
    if i mod 10 = 0 then
    response.write"</tr>"
 end if
      rs.movenext
    next
  end if
  response.write "</table>"
  rs.close
  set rs=nothing
 
  Function   RegExp_Execute(strng)  
          Dim   regEx,   Match,   Matches,values   '建立变量。  
          Set   regEx   =   New   RegExp   '建立正则表达式。 
          regEx.Pattern   =  "src\=.+?\.(gif|jpg)"   '设置模式。  
          regEx.IgnoreCase   =   true   '设置是否区分字符大小写。  
          regEx.Global   =   True   '设置全局可用性。  
          Set   Matches   =   regEx.Execute(strng)   '执行搜索。  
          For   Each   Match   in   Matches   '遍历匹配集合。  
                  values=values&Match.Value&""","  
          Next  
          RegExp_Execute   =   values  
    End   Function
%>

<%
str="图文新闻<IMG src=""/UploadFiles/20081027.jpg"" border=0>下一张图片<IMG src=""/UploadFiles/06.gif"">"
aa=RegExp_Execute(str)
response.write replace (aa,"src=","")
%>

<%
str="<img src=images/200610261533.jpg><img src=""images/200609151656.jpg""><img src=images/200609051735.JPG>"

response.write(GetImgSrc(str))


%>


企业开源网站管理系统,中小型企业的成长伙伴 支持(0中立(0反对(0回到顶部