在SQL中替换<img/>标签的width属性

时间:2018-08-30 09:24:28

标签: html sql-server image replace

我在数据库中的表包含500页的HTML。所有这些内容均包含<img width="100%".....

我想从所有图像中删除width =“ 100%”,而不影响其余内容。

例如 当前字符串

<img width="100%" src="/images/skyline.jpg" alt="Image showing sky line" />

<img src="/images/skyline.jpg" width="100%" alt="Image showing sky line" />

W3C验证将其检测为错误-“元素img上的属性宽度值错误100%:应为数字,但改为%。”

期望的字符串

<img src="/images/skyline.jpg" alt="Image showing sky line" />

2 个答案:

答案 0 :(得分:0)

该解决方案适用于Oracle DB,因为尚不清楚要搜索该解决方案的哪个DBMS

如果width始终位于img标记之后,则可以使用Replace函数在Oracle数据库中执行此操作,例如

replace (<column>,'<img width="100%" ','<img ');

更新语句可能像

update <table>
set <column> = replace (<column>,'<img width="100%" ','<img ')

如果属性width并非紧接在img标记之后,那么您必须首先找到IMG标记。您可以使用正则表达式执行此操作。 这里不止一次讨论了如何找到img标签。

check this question

用户sln的修改后的正则表达式可能对您有用:

 <img\s[^>]*?(width\s*=\s*[''\"]([^''\"]*?)[''\"])([^>]*?)>

首先,您必须找到img标签并过滤出信息

replace(REGEXP_SUBSTR(txt,'<img\s[^>]*?width\s*=\s*[''\"]([^''\"]*?)[''\"][^>]*?>'),'width="100%" ','') 

然后您可以将img标签替换为整个html中的过滤标签,如下所示:

REGEXP_REPLACE(txt
             , '<img\s[^>]*?(width\s*=\s*[''\"]([^''\"]*?)[''\"])([^>]*?)>'
             , replace(REGEXP_SUBSTR(txt,'<img\s[^>]*?width\s*=\s*[''\"]([^''\"]*?)[''\"][^>]*?>'),'width="100%" ','')          
             )

SQLFiddel Test

这可能不是最佳选择,但可能会有所帮助

答案 1 :(得分:0)

这将适用于sql server。我提供了一些示例img标签,以说明其工作原理。

但是请注意,它将重新格式化可能不理想的html。它将是相同的html,但没有多余的空格和换行符。这可能不是您想要的。

我必须这样做的原因是,当我转换为xml然后找到节点时,结果被修剪掉多余的空格,并且无法在原始字符串中找到它们。如果您的html在img标签中没有多余的空格,则可以省略此步骤:

select convert(varchar(max),convert(xml,x)) as trimmed from @t

如果这对您不起作用,则希望对匹配节点进行标识至少会有所帮助。只需在匹配的CTE之后添加select * from matching即可查看您所拥有的内容。

它也可以在逐个文档的基础上工作,因此您可能必须将其放在循环中才能遍历文档。或对其进行更新以在很多地方正常工作,但我想这是一次性操作,因此可能不需要。

declare @t table(x varchar(max))
insert @t values ('<html><div><img width="50%" /><img    width="100%" src="foo" alt="bar" />
<span class="c">sometext</span>
<div><img src="foo" alt="bah" width="100%"  /></div></div>
</html>')

;with [xml] as (
    --- convert the string to xml
    select convert(xml,x) as x from @t
)
,matching as (
    -- Find all the img nodes that have width=100%
    select convert(varchar(max),c.query('.')) as matches
    from [xml]
    cross apply x.nodes('//img[@width="100%"]') as t(c)
) 
,source as (
    -- Tidy up the source, as it has multiple spaces that prevent the matches from finding the nodes
    select convert(varchar(max),convert(xml,x)) as trimmed from @t
)
,replaced as (
    -- Work through recursively removing the 100% from matching nodes
    select trimmed from source
    union all
    select replace(trimmed,matches,replace(matches,'width="100%"','')) as replaced from matching
    cross join replaced
    where charindex(matches,replaced.trimmed)>0
)
-- Get the shortest (or last) string from above
select top 1 * from replaced order by len(trimmed) 

输出:

<html><div><img width="50%"/><img  src="foo" alt="bar"/><span class="c">sometext</span><div><img src="foo" alt="bah" /></div></div></html>
相关问题