[정규식] 재미난? 정규식 Again #16 - 속성편
그누보드 안에 있는 여러가지 정규식중 thumbnail관련 정규식을 찾아 봅니다.
lib/common.lib.php
[code]
// 에디터 이미지 얻기
function get_editor_image($contents, $view=true)
{
if(!$contents)
return false;
// $contents 중 img 태그 추출
if ($view)
$pattern = "/<img([^>]*)>/iS";
else
$pattern = "/<img[^>]*src=[\'\"]?([^>\'\"]+[^>\'\"]+)[\'\"]?[^>]*>/i";
preg_match_all($pattern, $contents, $matchs);
return $matchs;
}
[/code]
각 게시판에 포함된 이미지 태그를
lib/thumbnail.lib.php 에서
get_list_thumbnail
get_view_thumbnail 중에서.
[code]
$img = $matches[1][$i];
preg_match("/src=[\'\"]?([^>\'\"]+[^>\'\"]+)/i", $img, $m);
$src = $m[1];
preg_match("/style=[\"\']?([^\"\'>]+)/i", $img, $m);
$style = $m[1];
preg_match("/width:\s*(\d+)px/", $style, $m);
$width = $m[1];
preg_match("/height:\s*(\d+)px/", $style, $m);
$height = $m[1];
preg_match("/alt=[\"\']?([^\"\']*)[\"\']?/", $img, $m);
$alt = get_text($m[1]);
[/code]
각 게시판 글에 포함 된 이미지 태그중에서 attribute를 뽑아내는 정규식입니다.
src, style, alt를 width, height는 style안에서 뽑아냅니다.
각각을 찾아서 이미지는 썸네일을 만들고 원본이미지를 포함해서 아래와 같은 링크를 만듭니다.
[code]
<a href="//sir.kr/bbs/view_image.php?bo_table=cm_pix&fn=654829463_lOzcMPxZ_5b0f91e594696877dc2950317a83deb4eef1e1cd.jpeg" target="_blank" class="view_image">
<img src="//sir.kr/data/file/cm_pix/thumb-654829463_lOzcMPxZ_5b0f91e594696877dc2950317a83deb4eef1e1cd_730x548.jpg" alt="">
</a>
[/code]
대략 아래와 같은 이미지를 찾아서 뽑아내는 것입니다.
지금까지 배운 내용으로 한 문장의 정규식이 필요합니다. (위의 정규식도 참조)
그룹네임은 $src, $style, $width, $height, $alt 입니다.
그런데 이것이 정규식으로 가능할까요?
-----------------------------------
<img style="width:100%; height:100%;display:none" src="http://test.com/data/editor/testimage.jpg" alt="test_img">
<img alt="test_img" style="width:100%; height:100%;display:none" src="http://test.com/data/editor/testimage.jpg">
<img src="http://test.com/data/editor/testimage.jpg" style="width:100%; height:100%;display:none" alt="test_img">
-----------------------------------
게시판 목록
퀴즈게시판
채택은 '좋아요'와 같습니다.
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 1146 | 6년 전 | 5082 | ||
| 1145 | 6년 전 | 1948 | ||
| 1144 | 6년 전 | 1331 | ||
| 1143 | 6년 전 | 1423 | ||
| 1142 | 6년 전 | 1935 | ||
| 1141 | 6년 전 | 2121 | ||
| 1140 | 6년 전 | 1704 | ||
| 1139 | 6년 전 | 1503 | ||
| 1138 | 6년 전 | 1641 | ||
| 1137 | 6년 전 | 2198 | ||
| 1136 | 6년 전 | 1419 | ||
| 1135 | 6년 전 | 1585 | ||
| 1134 | 6년 전 | 4153 | ||
| 1133 | 6년 전 | 2893 | ||
| 1132 | 6년 전 | 9170 | ||
| 1131 | 6년 전 | 1843 | ||
| 1130 | 6년 전 | 1759 | ||
| 1129 | 6년 전 | 1798 | ||
| 1128 |
hyunbin
|
6년 전 | 4532 | |
| 1127 |
|
6년 전 | 1893 | |
| 1126 | 6년 전 | 2146 | ||
| 1125 | 6년 전 | 1888 | ||
| 1124 | 6년 전 | 1893 | ||
| 1123 | 6년 전 | 1727 | ||
| 1122 | 6년 전 | 1310 | ||
| 1121 | 6년 전 | 1848 | ||
| 1120 | 6년 전 | 1471 | ||
| 1119 | 6년 전 | 1990 | ||
| 1118 | 6년 전 | 1668 | ||
| 1117 | 6년 전 | 1463 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기