如何遍历所有新闻内容加网址?
在新闻数据表里即xinwen,有一字段新闻内容即nr,
nr字段里有一些图片,如图片地址为:tupian/1.jpg;
现在我要实现遍历所有nr字段,把图片地址都改为:http://www.xxxxx.com/tupian/1.jpg
即在图片地址前加域名,如何实现?
在新闻数据表里即xinwen,有一字段新闻内容即nr,
nr字段里有一些图片,如图片地址为:tupian/1.jpg;
现在我要实现遍历所有nr字段,把图片地址都改为:http://www.xxxxx.com/tupian/1.jpg
即在图片地址前加域名,如何实现?
这样应该可以的。
create table xinwen(
id int identity primary key,
nr nvarchar(500)
)
--测试数据--
insert into xinwen values('tupian/1.jpg')
insert into xinwen values('tupian/2.jpg');
insert into xinwen values('tupian/3.jpg');GO
--利用游标和Replace函数实现你的需求--
declare aa cursor for
select id,nr
from xinwen
open aa
declare @content nvarchar(500),@id int,@newContent nvarchar(500)
fetch next from aa into @id,@content
while(@@fetch_status=0)
begin
set @newContent = REPLACE(@content,'tupian/','http://www.xxxxx.com/tupian/')
update xinwen set nr = @newContent where id = @id
fetch next from aa into @id,@content
end
close aa
deallocate aa
GO本地测试成功
等高手 去问过客吧
1 http://www.xxxxx.com/tupian/1.jpg
2 http://www.xxxxx.com/tupian/2.jpg
3 http://www.xxxxx.com/tupian/3.jpg
$("img").each(function(index, item) {
item = $(item);
item.attr("src", yoururl+ item.attr("src"));
});
--------------
谢谢了,用你的方法是可以的。
但是有一个新问题,目前我的新闻内容字段是text
----------
我改成:
declare @content text(16),@id int,@newContent text(16)出错为“对于局部变量,text、ntext 和 image 数据类型无效。”
如何解决?
GO--利用游标和updatetext函数实现ntext/text的文本替换----当text文本小于4000的时候--
declare cr cursor for
select id,convert(nvarchar(4000), nr)
from xinwen
open cr
declare @content nvarchar(500),@id int,@newContent nvarchar(500)
fetch next from cr into @id,@content
while(@@fetch_status=0)
begin
set @newContent = REPLACE(@content,'tupian/','http://www.xxxxx.com/tupian/')
update xinwen set nr = @newContent where id = @id
fetch next from cr into @id,@content
end
close cr
deallocate crGO当文本很大时,你可以参照这个老贴:
http://topic.csdn.net/t/20040906/16/3345249.html
http://topic.csdn.net/u/20080420/19/F36FB7C0-B8A5-4D67-837C-9DC3A96907B8.html
http://topic.csdn.net/u/20090702/21/dc7e33c8-b495-4bcd-b56e-7edb35dc53ab.html
update tablename set nr = 'http://www.xxxxx.com/'+nr
insert into @t values( 'tupian/2.jpg')
insert into @t values( 'tupian/3.jpg')
update @t set nr = REPLACE( nr , nr , 'http://www.xxxxx.com/' + nr)select * from @t
declare @s_str varchar(8000),@d_str varchar(8000)
select @s_str='/huiyuan' --要替换的字符串
,@d_str='http://www.xxxx.com/abc' --替换成的字符串
--因为只能用patindex,所以对于搜索字符串做处理
set @s_str='%'+@s_str+'%'
--定义游标,循环处理数据
declare @id varchar(20)
declare #tb cursor for select cp_id from chanpin
open #tb
fetch next from #tb into @id
while @@fetch_status=0
begin
--字符串替换处理
declare @p varbinary(16)
,@p1 int,@p2 int
,@rplen int,@step int,@len int
select @p=textptr(cp_nr)
,@rplen=len(@s_str)-2
,@step=len(@d_str)
,@p1=patindex(@s_str,cp_nr)
,@len=datalength(cp_nr)
,@p2=0
from chanpin where cp_id=@id
while @p1>0
begin
set @p2=@p1+@p2-1
updatetext chanpin.cp_nr @p @p2 @rplen @d_str
select @p2=@p2+1,@p1=patindex(@s_str,substring(cp_nr,@p2+1,@len))
from chanpin where cp_id=@id
end
fetch next from #tb into @id
end
close #tb
deallocate #tb ===========================这个地方特别注意:
select @s_str='/huiyuan' --要替换的字符串
,@d_str='http://www.xxxx.com/abc' --替换成的字符串
--------------
如果这样替换是不能成功的,如:
select @s_str='/huiyuan' --要替换的字符串
,@d_str='http://www.xxxx.com/huiyuan' --替换成的字符串
以上代码会不断的替换下去,直到死机(无限循环)。
========================================
因为huiyuan这个文件夹是必须要保留的,我想了半天,用以下方式就可以实现了:
先执行这个代码:
select @s_str='/huiyuan' --要替换的字符串
,@d_str='http://www.xxxx.com/abc' --替换成的字符串 然后再执行,
select @s_str='http://www.xxxx.com/abc‘--要替换的字符串
,@d_str='http://www.xxxx.com/huiyuan' --替换成的字符串 就可以了