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 ,
본문내의 링크에 걸린 텍스트는? ==> 네이버 , ,
요런 정도 하면 되지 않을 까요
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 8030 | 9년 전 | 350 | ||
| 8029 | 9년 전 | 283 | ||
| 8028 | 9년 전 | 253 | ||
| 8027 | 9년 전 | 264 | ||
| 8026 | 9년 전 | 305 | ||
| 8025 | 9년 전 | 367 | ||
| 8024 | 9년 전 | 331 | ||
| 8023 | 9년 전 | 380 | ||
| 8022 | 9년 전 | 308 | ||
| 8021 | 9년 전 | 310 | ||
| 8020 | 9년 전 | 306 | ||
| 8019 | 9년 전 | 327 | ||
| 8018 | 9년 전 | 430 | ||
| 8017 | 9년 전 | 525 | ||
| 8016 | 9년 전 | 316 | ||
| 8015 | 9년 전 | 380 | ||
| 8014 | 9년 전 | 312 | ||
| 8013 | 9년 전 | 226 | ||
| 8012 | 9년 전 | 236 | ||
| 8011 | 9년 전 | 435 | ||
| 8010 | 9년 전 | 287 | ||
| 8009 | 9년 전 | 273 | ||
| 8008 | 9년 전 | 275 | ||
| 8007 | 9년 전 | 417 | ||
| 8006 | 9년 전 | 454 | ||
| 8005 |
|
9년 전 | 955 | |
| 8004 | 9년 전 | 339 | ||
| 8003 | 9년 전 | 409 | ||
| 8002 | 9년 전 | 316 | ||
| 8001 |
|
9년 전 | 655 | |
| 8000 | 9년 전 | 401 | ||
| 7999 | 9년 전 | 361 | ||
| 7998 | 9년 전 | 413 | ||
| 7997 | 9년 전 | 303 | ||
| 7996 | 9년 전 | 529 | ||
| 7995 | 9년 전 | 439 | ||
| 7994 | 9년 전 | 282 | ||
| 7993 | 9년 전 | 361 | ||
| 7992 | 9년 전 | 498 | ||
| 7991 | 9년 전 | 236 | ||
| 7990 | 9년 전 | 260 | ||
| 7989 | 9년 전 | 291 | ||
| 7988 | 9년 전 | 716 | ||
| 7987 | 9년 전 | 410 | ||
| 7986 | 9년 전 | 399 | ||
| 7985 | 9년 전 | 498 | ||
| 7984 | 9년 전 | 420 | ||
| 7983 | 9년 전 | 641 | ||
| 7982 | 9년 전 | 502 | ||
| 7981 | 9년 전 | 447 | ||
| 7980 | 9년 전 | 473 | ||
| 7979 | 9년 전 | 449 | ||
| 7978 | 9년 전 | 428 | ||
| 7977 | 9년 전 | 366 | ||
| 7976 | 9년 전 | 835 | ||
| 7975 | 9년 전 | 353 | ||
| 7974 | 9년 전 | 382 | ||
| 7973 | 9년 전 | 584 | ||
| 7972 | 9년 전 | 370 | ||
| 7971 | 9년 전 | 442 | ||
| 7970 | 9년 전 | 284 | ||
| 7969 | 9년 전 | 530 | ||
| 7968 | 9년 전 | 369 | ||
| 7967 | 9년 전 | 352 | ||
| 7966 | 9년 전 | 355 | ||
| 7965 |
|
9년 전 | 1003 | |
| 7964 | 9년 전 | 390 | ||
| 7963 | 9년 전 | 374 | ||
| 7962 | 9년 전 | 362 | ||
| 7961 |
전갈자리남자
|
9년 전 | 499 | |
| 7960 | 9년 전 | 948 | ||
| 7959 | 9년 전 | 541 | ||
| 7958 | 9년 전 | 396 | ||
| 7957 | 9년 전 | 358 | ||
| 7956 | 9년 전 | 364 | ||
| 7955 | 9년 전 | 432 | ||
| 7954 | 9년 전 | 355 | ||
| 7953 | 9년 전 | 417 | ||
| 7952 | 9년 전 | 341 | ||
| 7951 | 9년 전 | 486 | ||
| 7950 | 9년 전 | 374 | ||
| 7949 | 9년 전 | 380 | ||
| 7948 | 9년 전 | 315 | ||
| 7947 | 9년 전 | 918 | ||
| 7946 | 9년 전 | 410 | ||
| 7945 | 9년 전 | 378 | ||
| 7944 | 9년 전 | 386 | ||
| 7943 | 9년 전 | 358 | ||
| 7942 | 9년 전 | 395 | ||
| 7941 | 9년 전 | 379 | ||
| 7940 | 9년 전 | 869 | ||
| 7939 | 9년 전 | 321 | ||
| 7938 | 9년 전 | 362 | ||
| 7937 | 9년 전 | 262 | ||
| 7936 | 9년 전 | 868 | ||
| 7935 | 9년 전 | 426 | ||
| 7934 | 9년 전 | 393 | ||
| 7933 | 9년 전 | 471 | ||
| 7932 | 9년 전 | 453 | ||
| 7931 | 9년 전 | 508 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기