【问题标题】:Extract path from source(src) attribute in an HTML image tag(img)从 HTML 图像标签 (img) 中的源 (src) 属性中提取路径
【发布时间】:2019-02-15 17:17:10
【问题描述】:

我正在尝试从包含 HTML 的数据库字段中提取图像路径。数据库中的数据如下所示:

<p><strong><strong>DISCUSSION POINT</strong>: Nearly how many years did it take Sir Francis Drake to complete the first circumnavigation of the globe in 1580?&nbsp;<br /><p><img id="lk45459gjh4" src="../mediaForExam/dlfkeiut8484034djjd222.png" alt="dlfkeiut8484034djjd222.png" width="697" height="352" /></p>

从这个字符串中,我只需要这部分:

/mediaForExam/dlfkeiut8484034djjd222.png

我试过这个查询:

SELECT RIGHT(questionText, (LEN(questionText)-PATINDEX ( '%SRC="%' , questionText )-5)) AS MediaPath FROM exams.history

但它返回的字符串是这样的:

./mediaForExam/dlfkeiut8484034djjd222.png" alt="dlfkeiut8484034djjd222.png" width="697" height="352" />

有没有办法只返回第一个斜杠,文件夹名,然后是文件名(即:/folderName/fileName.ext)

谢谢!

【问题讨论】:

    标签: sql sql-server tsql sql-server-2012


    【解决方案1】:

    我愿意:

    select substring(hh.questionText, 1, charindex('"', hh.questionText) - 1)  as MediaPath 
    from exams.history h cross apply
         ( values (stuff(questionText, 1, charindex('src=', questionText)+6, '')) 
         ) hh (questionText);
    

    【讨论】:

    • 几乎完美!但是结果末尾有一个尾随引号(即 /path/image.png")。有没有办法删除它?谢谢
    • @SkyeBoniwell。 . .这就是查询的作用。重新复制查询并尝试。
    • 我在第一个子字符串调用 (substring(hh.questionText, 0, ...) 中将 1 更改为 0,它去掉了尾随引号。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-10-14
    • 2013-04-06
    • 2011-03-26
    • 1970-01-01
    • 2022-10-21
    • 2021-07-27
    • 1970-01-01
    • 2018-07-18
    相关资源
    最近更新 更多