ASP - html 중 img 태그 내부 속성값만 가져오기
제목 : html 중 img 태그 내부 속성값만 가져오기
<%
Set regEx = New RegExp ' 정규식을 작성합니다.
regEx.IgnoreCase = True ' 대/소문자 구분 안함을 설정합니다.
regEx.Global = True ' 전역을 설정합니다.
html = "aldskfja;dsfjka;dsfjka;fkjakldfjasdf<br>aljdsfhalfhalfjhalf<br><span>asdfasdfadsf</span><img src=""http://i2.media.daumcdn.net/photo-media/201104/15/ned/20110415101809573.jpg"" width=""500"" height=""646"" id=""1"">ㅁㄴ러미ㅏㅇ러마ㅣㄴㄹㅇ러<span>aksjdhfalkdfhlk</span><br><img src=""http://i2.media.daumcdn.net/photo-media/201104/15/ned/20110415101809573.jpg"" width=""500"" height=""646"" id=""2"">"
''' 1. 이미지 태그만 가져오기
regEx.Pattern = "<img [^<>]*>" ' 패턴을 설정합니다.
Set Matches = regEx.Execute(html) ' 찾기를 실행합니다.
RetStr = ""
RetImgPath = ""
RetWidth = ""
RetHeight = ""
RetId = ""
For Each Match in Matches ' Matches 컬렉션을 반복합니다.
RetStr = RetStr & "<br>" & Replace(Match.Value, "<", "<") & vbcrlf
file1 = mid(Match.Value, instrRev(Match.Value, "src=""") + 5)
filename = left(file1, instr(file1, """")-1)
file2 = mid(Match.Value, instrRev(Match.Value, "width=""") + 7)
strWidth = left(file2, instr(file2, """")-1)
file3 = mid(Match.Value, instrRev(Match.Value, "height=""") + 8)
strHeight = left(file3, instr(file3, """")-1)
file4 = mid(Match.Value, instrRev(Match.Value, "id=""") + 4)
strId = left(file4, instr(file4, """")-1)
RetImgPath = RetImgPath & filename & "<br>"
RetWidth = RetWidth & strWidth & "<br>"
RetHeight = RetHeight & strHeight & "<br>"
RetId = RetId & strId & "<br>"
Next
Response.Write "1. 이미지 태그만 가져오기<br>" & RetStr & "<br><br><br>"
Response.Write "2. 이미지 경로만 가져오기<br>" & RetImgPath & "<br><br><br>"
Response.Write "3. 이미지 가로길이만 가져오기<br>" & RetWidth & "<br><br><br>"
Response.Write "4. 이미지 세로길이만 가져오기<br>" & RetHeight & "<br><br><br>"
Response.Write "5. 이미지 ID값만 가져오기<br>" & RetId & "<br><br><br>"
%>
내용 : html 내용 중 <img> 태그의 속성값만 가져오기
소스보면 알것지만, instrRev 사용해서 알고자하는 속성값을 써주고, 글자 수만큼 + 해주면 끝
좀 멋지게 function 으로 만들어 보았다. 풉~
<%
Set regEx = New RegExp ' 정규식을 작성합니다.
regEx.IgnoreCase = True ' 대/소문자 구분 안함을 설정합니다.
regEx.Global = True ' 전역을 설정합니다.
html = "aldskfja;dsfjka;dsfjka;fkjakldfjasdf<br>aljdsfhalfhalfjhalf<br><span>asdfasdfadsf</span><img src=""http://i2.media.daumcdn.net/photo-media/201104/15/ned/20110415101809573.jpg"" width=""500"" height=""646"" id=""1"">ㅁㄴ러미ㅏㅇ러마ㅣㄴㄹㅇ러<span>aksjdhfalkdfhlk</span><br><img src=""http://i2.media.daumcdn.net/photo-media/201104/15/ned/20110415101809573.jpg"" width=""500"" height=""646"" id=""2"">"
''' 1. 이미지 태그만 가져오기
regEx.Pattern = "<img [^<>]*>" ' 패턴을 설정합니다.
Set Matches = regEx.Execute(html) ' 찾기를 실행합니다.
RetStr = ""
RetImgPath = ""
RetWidth = ""
RetHeight = ""
RetId = ""
For Each Match in Matches ' Matches 컬렉션을 반복합니다.
RetStr = RetStr & "<br>" & Replace(Match.Value, "<", "<") & vbcrlf
RetImgPath = RetImgPath & GetImgSrc(Match.Value, "src=""") & "<br>"
RetWidth = RetWidth & GetImgSrc(Match.Value, "width=""") & "<br>"
RetHeight = RetHeight & GetImgSrc(Match.Value, "height=""") & "<br>"
RetId = RetId & GetImgSrc(Match.Value, "id=""") & "<br>"
Next
Response.Write "1. 이미지 태그만 가져오기<br>" & RetStr & "<br><br><br>"
Response.Write "2. 이미지 경로만 가져오기<br>" & RetImgPath & "<br><br><br>"
Response.Write "3. 이미지 가로길이만 가져오기<br>" & RetWidth & "<br><br><br>"
Response.Write "4. 이미지 세로길이만 가져오기<br>" & RetHeight & "<br><br><br>"
Response.Write "5. 이미지 ID값만 가져오기<br>" & RetId & "<br><br><br>"
Function GetImgSrc(strImgTag, strAttr)
'strImgTag = <img src="asdfasdfaf.jpg" width="123" height="333" id="adsfasdf" />
'strAttr = src=", width="
Dim imgsrc, filepath
imgsrc = mid(strImgTag, instrRev(strImgTag, strAttr) + Len(strAttr))
filepath = left(imgsrc, instr(imgsrc, """")-1)
GetImgSrc = filepath
End Function
%>
결과
1. 이미지 태그만 가져오기
<img src="http://i2.media.daumcdn.net/photo-media/201104/15/ned/20110415101809573.jpg" width="500" height="646" id="1">
<img src="http://i2.media.daumcdn.net/photo-media/201104/15/ned/20110415101809573.jpg" width="500" height="646" id="2">
2. 이미지 경로만 가져오기
http://i2.media.daumcdn.net/photo-media/201104/15/ned/20110415101809573.jpg
http://i2.media.daumcdn.net/photo-media/201104/15/ned/20110415101809573.jpg
3. 이미지 가로길이만 가져오기
500
500
4. 이미지 세로길이만 가져오기
646
646
5. 이미지 ID값만 가져오기
1
2