当前位置: 移动技术网 > IT编程>开发语言>Asp > asp画中画广告插入在每篇文章中的实现方法

asp画中画广告插入在每篇文章中的实现方法

2017年12月01日  | 移动技术网IT编程  | 我要评论

尽管很多人给出了给每篇文章加上画中画广告的方法,但是这些所谓的方法,都不能真正地实现文字环绕在广告周围的“画中画”效果,只能左对其或者右对齐。现在要讨论的这个方法才能真正实现像新浪网、搜狐网那样的画中画广告效果。

首先说一下错误的div+css方法,希望大家别被误导:

<div id="outer" style="float:left;">
<div id="inner" style="float:left;margin:0;width:200px;height:200px;"></div>
文字内容
</div>

上面只能算作是左对齐的效果。还有很多借助表格或者iframe来实现的,也不外乎如此。

那么,怎么才能真正实现在每篇文章中批量添加画中画广告的效果呢?可以采用截取字段来进行,有两段代码。

第一段是分析文章内容字数,然后插入广告的代码:

dim leftcontent,midadcontent,rightcontent,modifycontent,headlen,tempstr,headadstr,tailadstr
''截取合适数量的字符串
if len(articlecontent)<320 then
 headlen=200
else
 headlen=320
end if
tempstr=articlecontent
leftcontent=interceptstring(tempstr,headlen)
'获得截取的文字内容
rightcontent=right(articlecontent,len(articlecontent)-len(leftcontent))
modifycontent=leftcontent &"<div style=""float:left;""><script language=""javascript"" src=""http://www.eryi.org/ad.js""></script></div>"& rightcontent

上面是通过div+js 来插入广告的,广告代码放在ad.js文件中,也可以通过table+js 或者直接用iframe的方式来插入。不管以那种方式,都必须设置其属性为左对齐或右对齐,这样才能保证为止环绕在广告周围。

接下的第二段就是画中画广告代码的判断了。

function interceptstring(txt,length)
dim x,y,ii,c,ischines,isascii,tempstr
txt=trim(txt)
x = len(txt)
y = 0
if x >= 1 then
 for ii = 1 to x
 c=asc(mid(txt,ii,1))
if c< 0 or c >255 then
  '说明是一个中文字符
 y = y + 2
 ischines=1
 isascii=0
else
  '说明是一个ascii码
 y = y + 1
 ischines=0
 isascii=1
end if
'如果长度已经大于定义子字符串长度,就判断是否包含敏感字符串是否分开
 if y >= length then
 if ischines=1 and strcount(left(trim(txt),ii),"<a")=strcount(left(trim(txt),ii),"</a>") then
 txt = left(trim(txt),ii) '"字符串限长
 exit for
  else
  if isascii=1 then x=x+1
  end if
 end if
 next
 interceptstring = txt
else
 interceptstring = ""
end if
end function
'判断字符串出现的次数
function strcount(str,substr) 
 dim istrcount
 dim istrstart
 dim itemp
 istrcount = 0
 istrstart = 1
 itemp = 0
 str=lcase(str)
 substr=lcase(substr)
 do while istrstart < len(str)
 itemp = instr(istrstart,str,substr,vbtextcompare)
 if itemp <=0 then
  istrstart = len(str)
 else
  istrstart = itemp + len(substr)
  
  istrcount = istrcount + 1
 end if
 loop
 strcount = istrcount
end function

以新云网站管理系统为例。首先找到生长静态文章页面的代码文件/inc/newschannel.asp (其它cms与此类似),在第248行 htmlcontent = replace(htmlcontent, "{$articlecontent}", articlecontent) 的前面插入第一段代码,在页面中的适当为止插入第二段代码,然后将该行中的articlecontent 改为modifycontent 即可。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网