我有以下代码检查SQL中的特定列,并确定它是图像还是文档工作正常。该代码中的列是Addon10。但是,我想扩展一下它将检查列中是否有多个图像。
目前,图像存储如下:
<img src="/images/pt1.png" style="width: 100%;">
<img src="/images/pt2.png" style="width: 100%;">
<img src="/images/pt3.png" style="width: 100%;">
<img src="/images/pt4.png" style="width: 100%;">
任何帮助都将不胜感激。
<%
if (!string.IsNullOrEmpty("Addon10")){
string grabExt = "Addon10".Substring("Addon10".LastIndexOf(".") + 1).ToLower().Remove(3);
if (grabExt == "png" || grabExt == "jpg" || grabExt == "jpeg" || grabExt == "tif" || grabExt == "tiff" || grabExt == "gif" || grabExt == "bmp")
Response.Write("<div style='page-break-before:always'>" + "Addon10" + "</div>");
else{
if (grabExt == "txt" || grabExt == "docx" || grabExt =="doc" || grabExt == "pdf" || grabExt== "rtf" || grabExt == ".xls" || grabExt == ".xlr" || grabExt == ".xlsx" || grabExt == "ods" || grabExt == "asp" || grabExt == "aspx")
Response.Write("<div style='page-break-before:always'>" + "Addon10" + "</div>");
}
}
%>
@Brad我做了以下事情:
<% string fileListString = "Addon10";
fileListString = fileListString.Replace("<img src=", ">|<img src=");
string[] arrTest = fileListString.Split('|');
foreach (string s in arrTest)
{
if (!string.IsNullOrEmpty("Addon10")){
string grabExt = "Addon10".Substring("Addon10".LastIndexOf(".") + 1).ToLower().Remove(3);
if (grabExt == "png" || grabExt == "jpg" || grabExt == "jpeg" || grabExt == "tif" || grabExt == "tiff" || grabExt == "gif" || grabExt == "bmp")
Response.Write("<div style='page-break-before:always'>" + "Addon10" + "</div>");
else{
if (grabExt == "txt" || grabExt == "docx" || grabExt =="doc" || grabExt == "pdf" || grabExt== "rtf" || grabExt == ".xls" || grabExt == ".xlr" || grabExt == ".xlsx" || grabExt == "ods" || grabExt == "asp" || grabExt == "aspx")
Response.Write("<div style='page-break-before:always'>" + "Addon10" + "</div>");
}
}
}
%>
答案 0 :(得分:1)
注意:我知道这不是最好的解决方案!它可以给你一个想法。 (最好的解决方案是不存储表中的数据,而是将每个图像/链接存储为自己的记录)。
它可能有效,但可能根本不起作用,具体取决于您的数据是否一致,但它在以下示例中有效。
// NOTE: in my string for testing there are double quotes in there and those are for escape characters.
// populate your string of files into a string variable
string fileListString = @"<img src=/""/images/pt1.png/"" style=/""width: 100%;/""><img src=/""/images/pt2.png/"" style=/""width: 100%;/""><img src=/""/images/pt3.png/"" style=/""width: 100%;/""><img src=/""/images/pt4.png/"" style=/""width: 100%;/"">";
// replace the closing HTML tag and Opening of the next img tag with a pipe (or you can change this to use a different delimiter
fileListString = fileListString.Replace("><img src=", ">|<img src=");
// then split the string on the pipe delimiter
string[] arrTest = fileListString.Split('|');
// then loop through each option
foreach (string s in arrTest)
{
// do your IF logic here
}
答案 1 :(得分:1)
您可以使用正则表达式测试列中标记中的多个图像:
var imageExtensions = new[] { "png", "jpg", "jpeg", "tif", "tiff", "gif", "bmp" };
var imageExtPattern = String.Join("|", imageExtensions);
var multiImagePattern = $"(^<img src=\"[^.]+\\.(?:{imageExtPattern})\".*>$)+";
var hasMultiImages = Regex.IsMatch(Addon10, multiImagePattern, RegexOptions.Multiline);