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 ,
본문내의 링크에 걸린 텍스트는? ==> 네이버 , ,
요런 정도 하면 되지 않을 까요
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 2330 | 16년 전 | 5069 | ||
| 2329 | 16년 전 | 2304 | ||
| 2328 | 16년 전 | 3404 | ||
| 2327 | 16년 전 | 2639 | ||
| 2326 | 16년 전 | 1556 | ||
| 2325 | 16년 전 | 5347 | ||
| 2324 | 16년 전 | 2465 | ||
| 2323 | 16년 전 | 5959 | ||
| 2322 | 16년 전 | 1936 | ||
| 2321 | 16년 전 | 4018 | ||
| 2320 | 16년 전 | 3027 | ||
| 2319 |
|
16년 전 | 2210 | |
| 2318 |
데니크레인
|
16년 전 | 2232 | |
| 2317 | 16년 전 | 4409 | ||
| 2316 | 16년 전 | 3292 | ||
| 2315 | 16년 전 | 2832 | ||
| 2314 | 16년 전 | 2959 | ||
| 2313 | 16년 전 | 2074 | ||
| 2312 | 16년 전 | 1764 | ||
| 2311 | 16년 전 | 1675 | ||
| 2310 | 16년 전 | 1395 | ||
| 2309 | 16년 전 | 1666 | ||
| 2308 | 16년 전 | 1902 | ||
| 2307 | 16년 전 | 1598 | ||
| 2306 |
|
16년 전 | 4848 | |
| 2305 | 16년 전 | 3825 | ||
| 2304 | 16년 전 | 1785 | ||
| 2303 | 16년 전 | 1529 | ||
| 2302 | 16년 전 | 2267 | ||
| 2301 | 16년 전 | 4528 | ||
| 2300 | 16년 전 | 3541 | ||
| 2299 | 16년 전 | 2575 | ||
| 2298 | 16년 전 | 4332 | ||
| 2297 | 16년 전 | 3203 | ||
| 2296 | 16년 전 | 1351 | ||
| 2295 |
|
16년 전 | 1375 | |
| 2294 |
|
16년 전 | 2037 | |
| 2293 | 16년 전 | 1881 | ||
| 2292 | 16년 전 | 2559 | ||
| 2291 | 16년 전 | 2231 | ||
| 2290 | 16년 전 | 1363 | ||
| 2289 | 16년 전 | 4391 | ||
| 2288 | 16년 전 | 1472 | ||
| 2287 | 16년 전 | 1780 | ||
| 2286 | 16년 전 | 2198 | ||
| 2285 |
|
16년 전 | 3832 | |
| 2284 |
|
16년 전 | 2729 | |
| 2283 |
|
16년 전 | 1483 | |
| 2282 | 16년 전 | 4011 | ||
| 2281 |
|
16년 전 | 1393 | |
| 2280 | 16년 전 | 1575 | ||
| 2279 | 16년 전 | 1234 | ||
| 2278 | 16년 전 | 1712 | ||
| 2277 | 16년 전 | 1611 | ||
| 2276 | 16년 전 | 1622 | ||
| 2275 | 16년 전 | 1423 | ||
| 2274 | 16년 전 | 2179 | ||
| 2273 | 16년 전 | 1816 | ||
| 2272 |
|
16년 전 | 1960 | |
| 2271 | 16년 전 | 2176 | ||
| 2270 |
a1system
|
16년 전 | 1469 | |
| 2269 | 16년 전 | 2303 | ||
| 2268 | 16년 전 | 2255 | ||
| 2267 | 16년 전 | 4527 | ||
| 2266 |
|
16년 전 | 1293 | |
| 2265 | 16년 전 | 1048 | ||
| 2264 |
잠자리똥꾸멍
|
16년 전 | 2009 | |
| 2263 |
태양의서쪽
|
16년 전 | 1971 | |
| 2262 |
태양의서쪽
|
16년 전 | 1515 | |
| 2261 |
태양의서쪽
|
16년 전 | 2037 | |
| 2260 |
|
16년 전 | 2269 | |
| 2259 | 16년 전 | 1383 | ||
| 2258 | 16년 전 | 1736 | ||
| 2257 | 16년 전 | 1921 | ||
| 2256 | 16년 전 | 1010 | ||
| 2255 | 16년 전 | 1688 | ||
| 2254 | 16년 전 | 1403 | ||
| 2253 | 16년 전 | 1910 | ||
| 2252 |
freedays
|
16년 전 | 1322 | |
| 2251 | 16년 전 | 2198 | ||
| 2250 | 16년 전 | 1878 | ||
| 2249 | 16년 전 | 5864 | ||
| 2248 | 16년 전 | 1911 | ||
| 2247 | 16년 전 | 3683 | ||
| 2246 | 16년 전 | 1466 | ||
| 2245 |
|
16년 전 | 1754 | |
| 2244 |
|
16년 전 | 2068 | |
| 2243 | 16년 전 | 2609 | ||
| 2242 | 16년 전 | 2304 | ||
| 2241 | 16년 전 | 2753 | ||
| 2240 | 16년 전 | 2989 | ||
| 2239 | 16년 전 | 3010 | ||
| 2238 |
|
16년 전 | 1820 | |
| 2237 |
|
16년 전 | 1538 | |
| 2236 |
|
16년 전 | 1411 | |
| 2235 | 16년 전 | 1723 | ||
| 2234 | 16년 전 | 1203 | ||
| 2233 | 16년 전 | 1959 | ||
| 2232 |
|
16년 전 | 1731 | |
| 2231 | 16년 전 | 1605 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기