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 ,
본문내의 링크에 걸린 텍스트는? ==> 네이버 , ,
요런 정도 하면 되지 않을 까요
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 2230 | 16년 전 | 1294 | ||
| 2229 | 16년 전 | 2162 | ||
| 2228 | 16년 전 | 3228 | ||
| 2227 | 16년 전 | 2173 | ||
| 2226 | 16년 전 | 1756 | ||
| 2225 | 16년 전 | 1519 | ||
| 2224 |
|
16년 전 | 2049 | |
| 2223 | 16년 전 | 4733 | ||
| 2222 | 16년 전 | 1417 | ||
| 2221 |
|
16년 전 | 2150 | |
| 2220 |
|
16년 전 | 2141 | |
| 2219 |
|
16년 전 | 2408 | |
| 2218 |
|
16년 전 | 2151 | |
| 2217 |
|
16년 전 | 4440 | |
| 2216 |
|
16년 전 | 2932 | |
| 2215 | 16년 전 | 2870 | ||
| 2214 | 16년 전 | 1606 | ||
| 2213 | 16년 전 | 1253 | ||
| 2212 |
|
16년 전 | 3956 | |
| 2211 |
|
16년 전 | 1624 | |
| 2210 |
|
16년 전 | 1622 | |
| 2209 | 16년 전 | 2116 | ||
| 2208 | 16년 전 | 1888 | ||
| 2207 |
letsgolee
|
16년 전 | 1500 | |
| 2206 |
|
16년 전 | 3929 | |
| 2205 | 16년 전 | 1768 | ||
| 2204 | 16년 전 | 3142 | ||
| 2203 | 16년 전 | 3029 | ||
| 2202 | 16년 전 | 1054 | ||
| 2201 | 16년 전 | 1847 | ||
| 2200 | 16년 전 | 1601 | ||
| 2199 |
|
16년 전 | 1861 | |
| 2198 |
태양의서쪽
|
16년 전 | 2657 | |
| 2197 |
태양의서쪽
|
16년 전 | 3032 | |
| 2196 |
태양의서쪽
|
16년 전 | 3233 | |
| 2195 | 16년 전 | 3331 | ||
| 2194 | 16년 전 | 1473 | ||
| 2193 |
letsgolee
|
16년 전 | 1429 | |
| 2192 | 17년 전 | 2260 | ||
| 2191 | 17년 전 | 1980 | ||
| 2190 |
|
17년 전 | 2293 | |
| 2189 | 17년 전 | 2814 | ||
| 2188 |
Sizkein
|
17년 전 | 3263 | |
| 2187 |
letsgolee
|
17년 전 | 2361 | |
| 2186 |
Sizkein
|
17년 전 | 1638 | |
| 2185 |
Sizkein
|
17년 전 | 3212 | |
| 2184 |
Sizkein
|
17년 전 | 3067 | |
| 2183 | 17년 전 | 3042 | ||
| 2182 | 17년 전 | 3924 | ||
| 2181 | 17년 전 | 2590 | ||
| 2180 |
|
17년 전 | 2411 | |
| 2179 | 17년 전 | 2656 | ||
| 2178 | 17년 전 | 2906 | ||
| 2177 | 17년 전 | 2621 | ||
| 2176 | 17년 전 | 1458 | ||
| 2175 | 17년 전 | 1900 | ||
| 2174 | 17년 전 | 1500 | ||
| 2173 |
|
17년 전 | 1926 | |
| 2172 | 17년 전 | 2528 | ||
| 2171 | 17년 전 | 8711 | ||
| 2170 | 17년 전 | 3166 | ||
| 2169 | 17년 전 | 4252 | ||
| 2168 |
|
17년 전 | 1995 | |
| 2167 | 17년 전 | 3662 | ||
| 2166 |
|
17년 전 | 1562 | |
| 2165 | 17년 전 | 1392 | ||
| 2164 | 17년 전 | 2330 | ||
| 2163 | 17년 전 | 1322 | ||
| 2162 | 17년 전 | 1644 | ||
| 2161 | 17년 전 | 1463 | ||
| 2160 | 17년 전 | 2533 | ||
| 2159 |
inxps
|
17년 전 | 1506 | |
| 2158 |
inxps
|
17년 전 | 1101 | |
| 2157 | 17년 전 | 1233 | ||
| 2156 | 17년 전 | 3113 | ||
| 2155 | 17년 전 | 2183 | ||
| 2154 | 17년 전 | 1984 | ||
| 2153 |
|
17년 전 | 1226 | |
| 2152 |
letsgolee
|
17년 전 | 2457 | |
| 2151 | 17년 전 | 1176 | ||
| 2150 |
아름다운세상
|
17년 전 | 1133 | |
| 2149 |
아름다운세상
|
17년 전 | 1381 | |
| 2148 |
아름다운세상
|
17년 전 | 2744 | |
| 2147 |
|
17년 전 | 2401 | |
| 2146 | 17년 전 | 2490 | ||
| 2145 | 17년 전 | 1191 | ||
| 2144 | 17년 전 | 1308 | ||
| 2143 | 17년 전 | 3255 | ||
| 2142 | 17년 전 | 2842 | ||
| 2141 | 17년 전 | 4113 | ||
| 2140 | 17년 전 | 2261 | ||
| 2139 | 17년 전 | 2351 | ||
| 2138 | 17년 전 | 1405 | ||
| 2137 | 17년 전 | 2519 | ||
| 2136 | 17년 전 | 2203 | ||
| 2135 | 17년 전 | 2963 | ||
| 2134 |
아름다운세상
|
17년 전 | 2473 | |
| 2133 |
Juinjang
|
17년 전 | 1637 | |
| 2132 | 17년 전 | 2435 | ||
| 2131 | 17년 전 | 1592 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기