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 ,
본문내의 링크에 걸린 텍스트는? ==> 네이버 , ,
요런 정도 하면 되지 않을 까요
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 2030 | 17년 전 | 2798 | ||
| 2029 | 17년 전 | 1603 | ||
| 2028 | 17년 전 | 1352 | ||
| 2027 | 17년 전 | 1611 | ||
| 2026 |
진정한승리
|
17년 전 | 2519 | |
| 2025 | 17년 전 | 1742 | ||
| 2024 | 17년 전 | 2020 | ||
| 2023 | 17년 전 | 1313 | ||
| 2022 |
SE7EN
|
17년 전 | 1049 | |
| 2021 | 17년 전 | 1127 | ||
| 2020 | 17년 전 | 2929 | ||
| 2019 | 17년 전 | 2474 | ||
| 2018 | 17년 전 | 2051 | ||
| 2017 | 17년 전 | 2052 | ||
| 2016 | 17년 전 | 1803 | ||
| 2015 | 17년 전 | 2254 | ||
| 2014 | 17년 전 | 2019 | ||
| 2013 | 17년 전 | 1987 | ||
| 2012 | 17년 전 | 1878 | ||
| 2011 | 17년 전 | 1218 | ||
| 2010 | 17년 전 | 1823 | ||
| 2009 | 17년 전 | 2231 | ||
| 2008 | 17년 전 | 2309 | ||
| 2007 | 17년 전 | 1642 | ||
| 2006 |
Arone
|
17년 전 | 1515 | |
| 2005 |
|
17년 전 | 1364 | |
| 2004 | 17년 전 | 2587 | ||
| 2003 |
suerte
|
17년 전 | 2682 | |
| 2002 |
suerte
|
17년 전 | 2083 | |
| 2001 |
suerte
|
17년 전 | 2424 | |
| 2000 |
|
17년 전 | 2729 | |
| 1999 |
|
17년 전 | 1331 | |
| 1998 |
|
17년 전 | 1198 | |
| 1997 | 17년 전 | 6335 | ||
| 1996 | 17년 전 | 1190 | ||
| 1995 | 17년 전 | 1633 | ||
| 1994 | 17년 전 | 1783 | ||
| 1993 | 17년 전 | 1111 | ||
| 1992 | 17년 전 | 1198 | ||
| 1991 | 17년 전 | 1898 | ||
| 1990 |
sdjsu
|
17년 전 | 1526 | |
| 1989 | 17년 전 | 3625 | ||
| 1988 |
smilesol
|
17년 전 | 1215 | |
| 1987 |
|
17년 전 | 1911 | |
| 1986 | 17년 전 | 1192 | ||
| 1985 | 17년 전 | 1394 | ||
| 1984 | 17년 전 | 2369 | ||
| 1983 | 17년 전 | 2028 | ||
| 1982 | 17년 전 | 1043 | ||
| 1981 | 17년 전 | 1049 | ||
| 1980 | 17년 전 | 1107 | ||
| 1979 |
오렌지76
|
17년 전 | 6054 | |
| 1978 |
오렌지76
|
17년 전 | 5852 | |
| 1977 | 17년 전 | 2042 | ||
| 1976 |
없어씨바라
|
17년 전 | 1033 | |
| 1975 | 17년 전 | 1121 | ||
| 1974 | 17년 전 | 2087 | ||
| 1973 | 17년 전 | 1520 | ||
| 1972 | 17년 전 | 1161 | ||
| 1971 |
smilesol
|
17년 전 | 1200 | |
| 1970 | 17년 전 | 1150 | ||
| 1969 |
smilesol
|
17년 전 | 1070 | |
| 1968 |
소행성블루
|
17년 전 | 3973 | |
| 1967 |
소행성블루
|
17년 전 | 2179 | |
| 1966 | 17년 전 | 1654 | ||
| 1965 |
소행성블루
|
17년 전 | 2781 | |
| 1964 |
고양Ol아빠
|
17년 전 | 1484 | |
| 1963 | 17년 전 | 1172 | ||
| 1962 | 17년 전 | 1190 | ||
| 1961 |
choijinhee
|
17년 전 | 1252 | |
| 1960 | 17년 전 | 1136 | ||
| 1959 | 17년 전 | 1256 | ||
| 1958 |
하이웹솔루션
|
17년 전 | 2409 | |
| 1957 | 17년 전 | 4194 | ||
| 1956 | 17년 전 | 1195 | ||
| 1955 | 17년 전 | 3955 | ||
| 1954 | 17년 전 | 4111 | ||
| 1953 | 17년 전 | 1238 | ||
| 1952 | 17년 전 | 2186 | ||
| 1951 | 17년 전 | 1624 | ||
| 1950 | 17년 전 | 3218 | ||
| 1949 | 17년 전 | 1368 | ||
| 1948 | 17년 전 | 3613 | ||
| 1947 | 17년 전 | 3406 | ||
| 1946 | 17년 전 | 2576 | ||
| 1945 | 17년 전 | 2940 | ||
| 1944 | 17년 전 | 2633 | ||
| 1943 | 17년 전 | 1951 | ||
| 1942 | 17년 전 | 3314 | ||
| 1941 | 17년 전 | 2629 | ||
| 1940 | 17년 전 | 3526 | ||
| 1939 |
|
17년 전 | 1166 | |
| 1938 | 17년 전 | 2251 | ||
| 1937 | 17년 전 | 1327 | ||
| 1936 |
smilesol
|
17년 전 | 1200 | |
| 1935 |
AHEEZII
|
17년 전 | 1065 | |
| 1934 |
smilesol
|
17년 전 | 1044 | |
| 1933 | 17년 전 | 1419 | ||
| 1932 | 17년 전 | 2861 | ||
| 1931 | 17년 전 | 3254 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기