리자

링크를 세부분으로 나누어 추출하려고 합니다.

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 함수와 정규식을 이용해 추출하고자 하는데,
정규식 작성이 잘 되지 않네요..

고수님들의 조언 부탁드립니다.<div class='small'>[이 게시물은 관리자님에 의해 2011-10-31 17:16:08 PHP & HTML에서 이동 됨]</div>
|

댓글 2개

내공이 부족해서 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>
preg_match_all 로 만들어 봤습니다.

preg_match_all("/(<a[^>]*href=[\"']?([^>\"'\s]+)[\"']?[^>]*>)([^<]*)/i", $s, $out1);

echo "<xmp>";
print_r($out1);
echo "</xmp>";

Array
(
[0] => Array
(
[0] => <a target=_blank href='http://www.naver.com'>네이버
[1] => <area shape="RECT" target="_blank" coords="10,10,135,60" href="http://www.daum.net" />
)

[1] => Array
(
[0] => <a target=_blank href='http://www.naver.com'>
[1] => <area shape="RECT" target="_blank" coords="10,10,135,60" href="http://www.daum.net" />
)

[2] => Array
(
[0] => http://www.naver.com
[1] => http://www.daum.net
)

[3] => Array
(
[0] => 네이버
[1] =>
)

)
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
15년 전 조회 1,783
15년 전 조회 2,131
15년 전 조회 1,562
15년 전 조회 7,758
15년 전 조회 1,189
15년 전 조회 1,409
15년 전 조회 1,131
15년 전 조회 1,353
15년 전 조회 1,120
15년 전 조회 1,333
15년 전 조회 1,051
15년 전 조회 2,157
15년 전 조회 1,167
15년 전 조회 1,077
15년 전 조회 1,559
15년 전 조회 1,454
15년 전 조회 1,178
15년 전 조회 1,050
15년 전 조회 1,089
15년 전 조회 1,378
15년 전 조회 954
15년 전 조회 1,027
15년 전 조회 1,225
15년 전 조회 1,929
15년 전 조회 1,245
15년 전 조회 1,330
15년 전 조회 1,101
15년 전 조회 1,044
15년 전 조회 1,421
15년 전 조회 1,866
15년 전 조회 1,141
15년 전 조회 1,478
15년 전 조회 1,759
15년 전 조회 1,112
15년 전 조회 1,037
15년 전 조회 1,192
15년 전 조회 1,259
15년 전 조회 1,065
15년 전 조회 1,351
15년 전 조회 1,426
15년 전 조회 1,335
15년 전 조회 3,261
15년 전 조회 939
15년 전 조회 1,496
15년 전 조회 1,078
15년 전 조회 1,893
15년 전 조회 858
15년 전 조회 1,007
15년 전 조회 1,011
15년 전 조회 1,029
15년 전 조회 1,760
15년 전 조회 3,076
15년 전 조회 2,152
15년 전 조회 2,052
15년 전 조회 1,052
15년 전 조회 1,158
15년 전 조회 1,096
15년 전 조회 977
15년 전 조회 1,275
15년 전 조회 984
15년 전 조회 1,425
15년 전 조회 870
15년 전 조회 782
15년 전 조회 1,653
15년 전 조회 921
15년 전 조회 1,364
15년 전 조회 1,229
15년 전 조회 904
15년 전 조회 929
15년 전 조회 834
15년 전 조회 809
15년 전 조회 987
15년 전 조회 1,227
15년 전 조회 2,400
15년 전 조회 995
15년 전 조회 1,082
15년 전 조회 1,117
15년 전 조회 1,116
15년 전 조회 1,186
15년 전 조회 803
15년 전 조회 2,055
15년 전 조회 898
15년 전 조회 1,704
15년 전 조회 852
15년 전 조회 864
15년 전 조회 1,535
15년 전 조회 1,147
15년 전 조회 1,444
15년 전 조회 1,758
15년 전 조회 1,381
15년 전 조회 1,483
15년 전 조회 851
15년 전 조회 2,394
15년 전 조회 1,126
15년 전 조회 862
15년 전 조회 1,210
15년 전 조회 912
15년 전 조회 2,180
15년 전 조회 1,203
15년 전 조회 1,015