[정규식] 재미난? 정규식 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">
-----------------------------------
게시판 목록
퀴즈게시판
채택은 '좋아요'와 같습니다.
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 1356 |
느긋한카키쿠키
|
3일 전 | 25 | |
| 1355 |
현대적인무질서한까마귀
|
3일 전 | 24 | |
| 1354 | 6개월 전 | 731 | ||
| 1353 | 6개월 전 | 636 | ||
| 1352 | 6개월 전 | 519 | ||
| 1351 | 7개월 전 | 608 | ||
| 1350 | 7개월 전 | 880 | ||
| 1349 | 7개월 전 | 652 | ||
| 1348 | 7개월 전 | 909 | ||
| 1347 | 7개월 전 | 557 | ||
| 1346 | 7개월 전 | 1459 | ||
| 1345 | 1년 전 | 1114 | ||
| 1344 | 1년 전 | 1663 | ||
| 1343 | 1년 전 | 1845 | ||
| 1342 | 1년 전 | 1036 | ||
| 1341 |
하늘그루터기
|
1년 전 | 1269 | |
| 1340 |
|
1년 전 | 1050 | |
| 1339 |
|
1년 전 | 888 | |
| 1338 |
|
1년 전 | 1282 | |
| 1337 |
tjddnjs
|
1년 전 | 1489 | |
| 1336 |
|
1년 전 | 2215 | |
| 1335 |
|
1년 전 | 1460 | |
| 1334 |
|
1년 전 | 2487 | |
| 1333 | 1년 전 | 1962 | ||
| 1332 | 1년 전 | 2063 | ||
| 1331 | 1년 전 | 1743 | ||
| 1330 | 1년 전 | 1314 | ||
| 1329 | 1년 전 | 1741 | ||
| 1328 | 2년 전 | 2002 | ||
| 1327 | 2년 전 | 1569 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기