html 코드내에서,
링크를 세부분으로 나누어 추출하려고 합니다.
다음과 같은 html 내용이 있을때,
**************************************************************
<a target=_blank href='http://www.naver.com'>네이버</a>
<area shape="RECT" target="_blank" coords="10,10,135,60" href="http://www.daum.net" />
**************************************************************
1. <a 혹은 <area 태그 내용
(예: <a target=_blank href='http://www.naver.com'> )
2. 1내용중 href의 내용
(href내용은 큰따옴표 혹은 작은따옴표로 감싸지거나 그냥 링크만 있을수도..)
3. <a 혹은 <area 의 텍스트내용
(위 html의 첫째줄에서는 네이버, 둘째줄에서는 공백이 되겠네요.)
이 세 내용을 preg_match_all 함수와 정규식을 이용해 추출하고자 하는데,
정규식 작성이 잘 되지 않네요..
고수님들의 조언 부탁드립니다.
링크를 세부분으로 나누어 추출하려고 합니다.
다음과 같은 html 내용이 있을때,
**************************************************************
<a target=_blank href='http://www.naver.com'>네이버</a>
<area shape="RECT" target="_blank" coords="10,10,135,60" href="http://www.daum.net" />
**************************************************************
1. <a 혹은 <area 태그 내용
(예: <a target=_blank href='http://www.naver.com'> )
2. 1내용중 href의 내용
(href내용은 큰따옴표 혹은 작은따옴표로 감싸지거나 그냥 링크만 있을수도..)
3. <a 혹은 <area 의 텍스트내용
(위 html의 첫째줄에서는 네이버, 둘째줄에서는 공백이 되겠네요.)
이 세 내용을 preg_match_all 함수와 정규식을 이용해 추출하고자 하는데,
정규식 작성이 잘 되지 않네요..
고수님들의 조언 부탁드립니다.
[이 게시물은 관리자님에 의해 2011-10-31 17:16:08 PHP & HTML에서 이동 됨]
댓글 3개
17년 전
내공이 부족해서 preg_match_all 은 못하겠네요.
<?
$s =<<<HTMLCODE
<a target=_blank href='http://www.naver.com'>네이버</a>
<area shape="RECT" target="_blank" coords="10,10,135,60" href="http://www.daum.net" />
HTMLCODE;
preg_match("/(\<a[^\>]*\>)/i", $s, $match1);
preg_match("/href\=[\"\']?([^\"\'\s\>]+)/i", $match1[1], $match2);
preg_match("/\<a[^\>]*\>(.*)\<\/a/i", $s, $match3);
?>
<textarea rows=10 cols=100><?print_r($match1)?></textarea>
<textarea rows=10 cols=100><?print_r($match2)?></textarea>
<textarea rows=10 cols=100><?print_r($match3)?></textarea>
<?
$s =<<<HTMLCODE
<a target=_blank href='http://www.naver.com'>네이버</a>
<area shape="RECT" target="_blank" coords="10,10,135,60" href="http://www.daum.net" />
HTMLCODE;
preg_match("/(\<a[^\>]*\>)/i", $s, $match1);
preg_match("/href\=[\"\']?([^\"\'\s\>]+)/i", $match1[1], $match2);
preg_match("/\<a[^\>]*\>(.*)\<\/a/i", $s, $match3);
?>
<textarea rows=10 cols=100><?print_r($match1)?></textarea>
<textarea rows=10 cols=100><?print_r($match2)?></textarea>
<textarea rows=10 cols=100><?print_r($match3)?></textarea>
17년 전
http://phpschool.com/gnuboard4/bbs/board.php?bo_table=qna_function&wr_id=241200&sca=&sfl=mb_id&stx=kagla&sop=and
17년 전
<?
$s =<<<HTMLCODE
<a target=_blank href='http://www.naver.com'>네이버</a>
<area shape="RECT" target="_blank" coords="10,10,135,60" href="http://www.daum.net" />
HTMLCODE;
preg_match_all("`<\s*(a[^>\s]*)[^>]* href\s*=\s*([^\s>]+)[^>]*(>(.+)</\s*\\1\s*>|/>)`is", $s, $match);
//print_r($match);
if (is_array($match[1])){
foreach($match[1] as $k => $v){
$text1 .= "$v , ";//태그들
$text2 .= str_replace(array("'", '"'), '', $match[2][$k]) . " , ";//링크들
$text3 .= trim($match[4][$k]) . " , ";//링크에 걸린 텍스트
}
}
echo "본문내의 태그들은? ==> $text1 <br>";
echo "본문내의 링크들은? ==> $text2 <br>";
echo "본문내의 링크에 걸린 텍스트는? ==> $text3 <br>";
?>
결과값
본문내의 태그들은? ==> a , area ,
본문내의 링크들은? ==> http://www.naver.com , http://www.daum.net ,
본문내의 링크에 걸린 텍스트는? ==> 네이버 , ,
요런 정도 하면 되지 않을 까요
$s =<<<HTMLCODE
<a target=_blank href='http://www.naver.com'>네이버</a>
<area shape="RECT" target="_blank" coords="10,10,135,60" href="http://www.daum.net" />
HTMLCODE;
preg_match_all("`<\s*(a[^>\s]*)[^>]* href\s*=\s*([^\s>]+)[^>]*(>(.+)</\s*\\1\s*>|/>)`is", $s, $match);
//print_r($match);
if (is_array($match[1])){
foreach($match[1] as $k => $v){
$text1 .= "$v , ";//태그들
$text2 .= str_replace(array("'", '"'), '', $match[2][$k]) . " , ";//링크들
$text3 .= trim($match[4][$k]) . " , ";//링크에 걸린 텍스트
}
}
echo "본문내의 태그들은? ==> $text1 <br>";
echo "본문내의 링크들은? ==> $text2 <br>";
echo "본문내의 링크에 걸린 텍스트는? ==> $text3 <br>";
?>
결과값
본문내의 태그들은? ==> a , area ,
본문내의 링크들은? ==> http://www.naver.com , http://www.daum.net ,
본문내의 링크에 걸린 텍스트는? ==> 네이버 , ,
요런 정도 하면 되지 않을 까요
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 8230 | 9년 전 | 43 | ||
| 8229 | 9년 전 | 51 | ||
| 8228 |
커네드커네드
|
9년 전 | 94 | |
| 8227 | 9년 전 | 104 | ||
| 8226 | 9년 전 | 141 | ||
| 8225 | 9년 전 | 132 | ||
| 8224 | 9년 전 | 127 | ||
| 8223 | 9년 전 | 87 | ||
| 8222 |
|
9년 전 | 161 | |
| 8221 | 9년 전 | 77 | ||
| 8220 | 9년 전 | 71 | ||
| 8219 | 9년 전 | 76 | ||
| 8218 | 9년 전 | 111 | ||
| 8217 |
star3840
|
9년 전 | 95 | |
| 8216 | 9년 전 | 131 | ||
| 8215 | 9년 전 | 84 | ||
| 8214 | 9년 전 | 202 | ||
| 8213 | 9년 전 | 141 | ||
| 8212 | 9년 전 | 54 | ||
| 8211 | 9년 전 | 220 | ||
| 8210 | 9년 전 | 213 | ||
| 8209 | 9년 전 | 315 | ||
| 8208 | 9년 전 | 184 | ||
| 8207 | 9년 전 | 197 | ||
| 8206 |
|
9년 전 | 159 | |
| 8205 | 9년 전 | 143 | ||
| 8204 | 9년 전 | 107 | ||
| 8203 | 9년 전 | 198 | ||
| 8202 | 9년 전 | 123 | ||
| 8201 | 9년 전 | 162 | ||
| 8200 | 9년 전 | 133 | ||
| 8199 | 9년 전 | 177 | ||
| 8198 | 9년 전 | 152 | ||
| 8197 | 9년 전 | 132 | ||
| 8196 | 9년 전 | 511 | ||
| 8195 | 9년 전 | 133 | ||
| 8194 | 9년 전 | 265 | ||
| 8193 | 9년 전 | 131 | ||
| 8192 | 9년 전 | 159 | ||
| 8191 | 9년 전 | 115 | ||
| 8190 | 9년 전 | 108 | ||
| 8189 | 9년 전 | 163 | ||
| 8188 | 9년 전 | 105 | ||
| 8187 | 9년 전 | 119 | ||
| 8186 | 9년 전 | 128 | ||
| 8185 | 9년 전 | 291 | ||
| 8184 | 9년 전 | 82 | ||
| 8183 | 9년 전 | 306 | ||
| 8182 | 9년 전 | 137 | ||
| 8181 | 9년 전 | 109 | ||
| 8180 | 9년 전 | 676 | ||
| 8179 | 9년 전 | 471 | ||
| 8178 | 9년 전 | 276 | ||
| 8177 |
kiplayer
|
9년 전 | 283 | |
| 8176 | 9년 전 | 322 | ||
| 8175 | 9년 전 | 203 | ||
| 8174 | 9년 전 | 210 | ||
| 8173 | 9년 전 | 321 | ||
| 8172 | 9년 전 | 164 | ||
| 8171 | 9년 전 | 159 | ||
| 8170 | 9년 전 | 275 | ||
| 8169 |
커네드커네드
|
9년 전 | 243 | |
| 8168 | 9년 전 | 295 | ||
| 8167 | 9년 전 | 303 | ||
| 8166 | 9년 전 | 214 | ||
| 8165 | 9년 전 | 146 | ||
| 8164 | 9년 전 | 276 | ||
| 8163 | 9년 전 | 264 | ||
| 8162 | 9년 전 | 270 | ||
| 8161 | 9년 전 | 269 | ||
| 8160 |
|
9년 전 | 465 | |
| 8159 | 9년 전 | 381 | ||
| 8158 | 9년 전 | 200 | ||
| 8157 | 9년 전 | 339 | ||
| 8156 | 9년 전 | 254 | ||
| 8155 | 9년 전 | 231 | ||
| 8154 |
00년생용띠
|
9년 전 | 576 | |
| 8153 | 9년 전 | 203 | ||
| 8152 |
|
9년 전 | 380 | |
| 8151 | 9년 전 | 382 | ||
| 8150 | 9년 전 | 472 | ||
| 8149 |
Jangfolk
|
9년 전 | 313 | |
| 8148 | 9년 전 | 147 | ||
| 8147 | 9년 전 | 356 | ||
| 8146 | 9년 전 | 409 | ||
| 8145 | 9년 전 | 339 | ||
| 8144 | 9년 전 | 305 | ||
| 8143 | 9년 전 | 161 | ||
| 8142 | 9년 전 | 406 | ||
| 8141 | 9년 전 | 355 | ||
| 8140 | 9년 전 | 904 | ||
| 8139 | 9년 전 | 228 | ||
| 8138 |
전갈자리남자
|
9년 전 | 368 | |
| 8137 | 9년 전 | 349 | ||
| 8136 | 9년 전 | 714 | ||
| 8135 |
|
9년 전 | 763 | |
| 8134 |
PlayPixel
|
9년 전 | 477 | |
| 8133 |
|
9년 전 | 418 | |
| 8132 | 9년 전 | 429 | ||
| 8131 | 9년 전 | 784 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기