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 ,
본문내의 링크에 걸린 텍스트는? ==> 네이버 , ,
요런 정도 하면 되지 않을 까요
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7830 | 9년 전 | 377 | ||
| 7829 |
|
9년 전 | 554 | |
| 7828 | 9년 전 | 489 | ||
| 7827 | 9년 전 | 372 | ||
| 7826 | 9년 전 | 381 | ||
| 7825 | 9년 전 | 434 | ||
| 7824 | 9년 전 | 417 | ||
| 7823 | 9년 전 | 322 | ||
| 7822 | 9년 전 | 317 | ||
| 7821 | 9년 전 | 262 | ||
| 7820 | 9년 전 | 318 | ||
| 7819 |
|
9년 전 | 720 | |
| 7818 | 10년 전 | 335 | ||
| 7817 | 10년 전 | 454 | ||
| 7816 | 10년 전 | 357 | ||
| 7815 | 10년 전 | 561 | ||
| 7814 | 10년 전 | 385 | ||
| 7813 | 10년 전 | 322 | ||
| 7812 | 10년 전 | 345 | ||
| 7811 | 10년 전 | 362 | ||
| 7810 | 10년 전 | 494 | ||
| 7809 | 10년 전 | 435 | ||
| 7808 | 10년 전 | 298 | ||
| 7807 | 10년 전 | 358 | ||
| 7806 |
프로그래머7
|
10년 전 | 1307 | |
| 7805 | 10년 전 | 1217 | ||
| 7804 |
zahir1312
|
10년 전 | 744 | |
| 7803 |
|
10년 전 | 1344 | |
| 7802 | 10년 전 | 402 | ||
| 7801 | 10년 전 | 829 | ||
| 7800 | 10년 전 | 1047 | ||
| 7799 | 10년 전 | 505 | ||
| 7798 | 10년 전 | 452 | ||
| 7797 | 10년 전 | 452 | ||
| 7796 | 10년 전 | 303 | ||
| 7795 | 10년 전 | 455 | ||
| 7794 | 10년 전 | 478 | ||
| 7793 | 10년 전 | 998 | ||
| 7792 | 10년 전 | 406 | ||
| 7791 | 10년 전 | 489 | ||
| 7790 | 10년 전 | 453 | ||
| 7789 |
fbastore
|
10년 전 | 1403 | |
| 7788 | 10년 전 | 487 | ||
| 7787 | 10년 전 | 353 | ||
| 7786 | 10년 전 | 503 | ||
| 7785 | 10년 전 | 521 | ||
| 7784 | 10년 전 | 591 | ||
| 7783 | 10년 전 | 392 | ||
| 7782 | 10년 전 | 448 | ||
| 7781 | 10년 전 | 850 | ||
| 7780 | 10년 전 | 776 | ||
| 7779 | 10년 전 | 746 | ||
| 7778 | 10년 전 | 316 | ||
| 7777 | 10년 전 | 398 | ||
| 7776 | 10년 전 | 403 | ||
| 7775 | 10년 전 | 339 | ||
| 7774 | 10년 전 | 601 | ||
| 7773 | 10년 전 | 322 | ||
| 7772 | 10년 전 | 667 | ||
| 7771 | 10년 전 | 327 | ||
| 7770 | 10년 전 | 612 | ||
| 7769 | 10년 전 | 331 | ||
| 7768 | 10년 전 | 548 | ||
| 7767 | 10년 전 | 1115 | ||
| 7766 | 10년 전 | 444 | ||
| 7765 | 10년 전 | 476 | ||
| 7764 |
잘살아보자
|
10년 전 | 326 | |
| 7763 |
|
10년 전 | 1400 | |
| 7762 |
Tosea
|
10년 전 | 1018 | |
| 7761 | 10년 전 | 614 | ||
| 7760 |
잘살아보자
|
10년 전 | 601 | |
| 7759 |
잘살아보자
|
10년 전 | 415 | |
| 7758 |
잘살아보자
|
10년 전 | 532 | |
| 7757 | 10년 전 | 1179 | ||
| 7756 |
ITBANK
|
10년 전 | 1217 | |
| 7755 | 10년 전 | 1894 | ||
| 7754 | 10년 전 | 998 | ||
| 7753 | 10년 전 | 843 | ||
| 7752 | 10년 전 | 1348 | ||
| 7751 |
잘살아보자
|
10년 전 | 472 | |
| 7750 |
잘살아보자
|
10년 전 | 441 | |
| 7749 |
잘살아보자
|
10년 전 | 437 | |
| 7748 |
잘살아보자
|
10년 전 | 420 | |
| 7747 |
잘살아보자
|
10년 전 | 509 | |
| 7746 |
잘살아보자
|
10년 전 | 633 | |
| 7745 |
잘살아보자
|
10년 전 | 875 | |
| 7744 |
잘살아보자
|
10년 전 | 387 | |
| 7743 | 10년 전 | 912 | ||
| 7742 |
starbros
|
10년 전 | 786 | |
| 7741 |
잘살아보자
|
10년 전 | 604 | |
| 7740 |
잘살아보자
|
10년 전 | 479 | |
| 7739 |
잘살아보자
|
10년 전 | 433 | |
| 7738 |
잘살아보자
|
10년 전 | 488 | |
| 7737 |
잘살아보자
|
10년 전 | 446 | |
| 7736 |
잘살아보자
|
10년 전 | 466 | |
| 7735 |
잘살아보자
|
10년 전 | 801 | |
| 7734 |
잘살아보자
|
10년 전 | 393 | |
| 7733 |
잘살아보자
|
10년 전 | 493 | |
| 7732 |
잘살아보자
|
10년 전 | 650 | |
| 7731 |
잘살아보자
|
10년 전 | 578 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기