6月7

正则表达式统计文章内容中图片数量

| |
14:29 ASP  From: 本站原创
正则表达式统计文章内容中图片数量
ASP版本的,经过测试:

'文章内容中的字数
Function ShowImgCount(content,ifincludepic)
    If ifincludepic = 0  Then
        Exit Function
    Else
        Dim Reg,Cols,Mat,str
        set Reg = new RegExp
        Reg.Global = True
        Reg.IgnoreCase = True
        Reg.MultiLine = True
        Reg.Pattern = "<img[^>]+src=""([^"">]+)""[^>]*>"
        Set Cols = Reg.Execute(content)
        For Each Mat In Cols
        str =Mat.SubMatches(0)
        ShowImgCount=ShowImgCount+1
        Next
        ShowImgCount="<span style='color:green'>"&ShowImgCount&"</span>图 "
    End If
End Function

另ASP获取文章内容中第一张图片

Function Frist_Pic(ByVal t0)
Frist_Pic=""
Dim Regs,Matches
Set Regs=New RegExp
Regs.Ignorecase=True
Regs.Global=True
Regs.Pattern="<img[^>]+src=""([^"">]+)""[^>]*>"
Set Matches=Regs.Execute(t0)
IF Regs.test(t0) Then
Frist_Pic=Matches(0).SubMatches(0)
End IF
Set Matches=Nothing
Set Regs=Nothing
End Function

PHP版本,网路上说是帝国CMS的方法

function user_FieldFun($mid,$f,$isadd,$isq,$value,$cs){
$pattern="/<img.*?src=['|"](.*?(?:[.gif|.jpg]))['|"].*?[/]?>/"; //正则
$str=$_POST['newstext']; //接收到正文的内容
preg_match_all($pattern,$str,$match); //使用PHP正则函数,返回的是数组
//if(count($match[1])>0){//这个是用来判断是否有图片存在
//foreach ( $match[0] as $src) {//使用foreach来遍历数组,显示出图片地址
// $a.=$src; //把地址放到变量$a里
// }

备注:如果不准请调整正则表达式
正则表达式统计文章内容中图片数量 asp图片正则 统计文章中图片个数 正则表达式统计图片数量
阅读(2189) | 评论(0) | 引用(0)
在此留下酱油瓶-:)
表情
emotemotemotemotemotemotemotemotemotemotemotemotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我

您的大名(例如:小明) : 

密码(可不填) :  游客无需密码

网址 (可不填) : 

电邮 (可不填) :  [注册]