[정규식] 재미난? 정규식 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">
-----------------------------------
게시판 목록
퀴즈게시판
채택은 '좋아요'와 같습니다.
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 1296 | 5년 전 | 1430 | ||
| 1295 | 5년 전 | 1516 | ||
| 1294 | 5년 전 | 1953 | ||
| 1293 | 5년 전 | 2158 | ||
| 1292 | 5년 전 | 2583 | ||
| 1291 | 5년 전 | 1422 | ||
| 1290 | 5년 전 | 2154 | ||
| 1289 | 5년 전 | 1977 | ||
| 1288 | 5년 전 | 1859 | ||
| 1287 | 5년 전 | 1336 | ||
| 1286 | 5년 전 | 1467 | ||
| 1285 | 5년 전 | 1751 | ||
| 1284 | 5년 전 | 1526 | ||
| 1283 | 5년 전 | 1517 | ||
| 1282 | 5년 전 | 2258 | ||
| 1281 | 5년 전 | 1961 | ||
| 1280 | 5년 전 | 1566 | ||
| 1279 | 5년 전 | 1597 | ||
| 1278 | 5년 전 | 1243 | ||
| 1277 | 5년 전 | 1211 | ||
| 1276 | 5년 전 | 2980 | ||
| 1275 |
|
5년 전 | 1395 | |
| 1274 |
|
5년 전 | 1416 | |
| 1273 | 5년 전 | 1987 | ||
| 1272 | 5년 전 | 2291 | ||
| 1271 | 5년 전 | 1200 | ||
| 1270 |
|
5년 전 | 1565 | |
| 1269 |
|
5년 전 | 1642 | |
| 1268 |
|
5년 전 | 4387 | |
| 1267 |
언제마스터합니까
|
5년 전 | 1840 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기