/** 给content的img添加a
* @param content
* @return
*/
private String updateContent(String content) {
StringBuffer result = new StringBuffer();
String temp = "";
int begin = content.toLowerCase().indexOf("<img");
while(begin != -1) {
result.append(content.substring(0, begin));
temp = content.substring(begin);
int imgSrcBegin = temp.toLowerCase().indexOf("src=") + 5;
String imgUrl = temp.substring(imgSrcBegin, imgSrcBegin + 50);
imgUrl = imgUrl.toLowerCase().replace("/s/", "/");
imgUrl = "<a rel=\"example_group\" href=\""+imgUrl+"\" >";
result.append(imgUrl);
int imgEnd = temp.toLowerCase().indexOf("/>") + 2;
String img = temp.substring(0, imgEnd);
result.append(img);
result.append("</a>");
content = temp.substring(imgEnd, temp.length());
begin = content.toLowerCase().indexOf("<img");
if(begin == -1)
result.append(content);
}
return result.toString();
}
请高手指教
ASP
ASP.NET
String
------解决方案--------------------
function updateContent(content)
if trim(content)="" then exit function
set rx=new regexp
rx.IgnoreCase=true
rx.Global=true
rx.Pattern="(<img.*?src=['""]([^'""]+)\1[^>]*>)"
updateContent=rx.Replace(content,"<a rel=""example_group"" href=""$2"">$2$1</a>")
set rx=nothing
end function
msgbox updatecontent("afe<img src='11111'/>123afe<img src='2222'/>123afe<img src='333'/>123")
你测试不就知道了,就是用正则找出img标签并提取src属性,然后给图片加上链接和你要的图片地址