html - sql 从存储的 html 中提取所有 img src 标签

标签 html sql image sql-server-2008 substring

好吧...这应该是相当简单的,但我无法理解最简单的方法。

我想对我的数据库运行查询,查看“描述”并提取任何内容 标记,以便我可以将它们放入单独的表中...

对我来说,逻辑似乎是首先找到 img 标签的起点,然后找到结束标签并从头到尾获取子字符串...但是,我也有

标签,所以我可以简单地获取< 的索引并将其作为我的开始,因为它也会拾取 p 标签...... 所以我是否必须获取 < 的索引,然后采用 2 的子字符串来检查它是否是 p 或 img 或 href 或其他什么,然后仅在与 img 匹配时才采用子字符串,或者是否有更简单的方法可以让我我完全失踪了??

我想我应该提到的另一点是,一旦我将 img 标签放入新表中,我还想保存不包含它们的描述。

最佳答案

好吧...所以经过整个早上的思考...我终于想出了一个可行的解决方案,所以我想我会分享它...

我需要什么:一种循环遍历我的产品描述并提取属于描述 html 的所有图像的方法,例如: Logo 或产品图像等......并将它们放入自己的表中,并与产品,然后将其从说明中删除。有些产品有多个图像,有些产品只有一个图像。

解决办法:

declare @prodId int
declare getproduct cursor for select Id from Product where IsActive = 1 and Description like '%<img src%'
declare @imgString varchar(200)
declare @endPos int
declare @desc varchar(max)
declare @position int
declare @outputDesc varchar(max)

    open getproduct
fetch next from getproduct into @prodId

while @@FETCH_STATUS = 0
begin
            -- get product Id
    select @desc = Description from Product where Id = @prodId

  --gets the index of the first instance of <img
    select @position = CHARINDEX('<img', @desc)
    while @position < len(@desc)
    begin
        --this assumes that we are not at the end of the description field
        if (SUBSTRING(@desc, @position, 4) = '<img')
        begin

                select @endPos = charIndex('>', substring(@desc, @position, 200))
                select @imgString = substring(@desc, @position, @endPos)



                    insert into dbo.ProductImage(ProductId, ImageUrl, DisplayName, IsPrimaryImage)
                    select @prodId, @imgString, DisplayName, 0 
                    from Product where Id = @prodId
                                 and not exists (select ImageUrl from ProductImage where ProductId = @prodId and ImageUrl = @imgString)


                select @outputDesc = REPLACE(@desc, @imgString, '')
                select @outputDesc = Replace(@outputDesc, '</img>', '')

                select @position = @endPos  

        end
        else
        begin
                           -- if we have reached here, there are no more instances of <img 
            -- set @position to end of description field to prevent continuous looping
            select @position = len(@desc)
        end




    end 

    select @outputDesc

    update Product 
    set Description = @outputDesc
    where Id = @prodId

    select Description from Product where Id = @prodId



fetch next from getproduct into @prodId
end

close getproduct
deallocate getproduct

这可能不是最迷人的方式,并且可能会稍微整理一下,但它确实有效......

关于html - sql 从存储的 html 中提取所有 img src 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21587136/

相关文章:

python - 如何在不使用 imageio 或 scikit 图像的情况下将图像读入脚本?

image - 如何去除 PDF 图像中的抗锯齿?

html - 将画廊中的图像与悬停在其上的图像叠加

c# - 使用 HtmlAgilityPack 计算特定的子节点

html - 填充在 IE7 和 IE8 中不起作用

html - 如何将我的物化下拉按钮放在右边?

SQL - EXISTS 未引入子查询

html - 用户点击/触摸 div 时的 CSS 框阴影效果

mysql - 从具有多个条件的多个表中进行选择

mysql - 对多个组进行分组的 SQL 查询