리자

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

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,738
15년 전 조회 2,084
15년 전 조회 1,516
15년 전 조회 7,708
15년 전 조회 1,145
15년 전 조회 1,363
15년 전 조회 1,083
15년 전 조회 1,313
15년 전 조회 1,080
15년 전 조회 1,293
15년 전 조회 1,018
15년 전 조회 2,121
15년 전 조회 1,130
15년 전 조회 1,038
15년 전 조회 1,523
15년 전 조회 1,410
15년 전 조회 1,145
15년 전 조회 1,015
15년 전 조회 1,046
15년 전 조회 1,347
15년 전 조회 918
15년 전 조회 991
15년 전 조회 1,188
15년 전 조회 1,883
15년 전 조회 1,202
15년 전 조회 1,286
15년 전 조회 1,068
15년 전 조회 1,005
15년 전 조회 1,379
15년 전 조회 1,835
15년 전 조회 1,111
15년 전 조회 1,442
15년 전 조회 1,723
15년 전 조회 1,074
15년 전 조회 1,004
15년 전 조회 1,154
15년 전 조회 1,223
15년 전 조회 1,031
15년 전 조회 1,318
15년 전 조회 1,381
15년 전 조회 1,311
15년 전 조회 3,234
15년 전 조회 908
15년 전 조회 1,466
15년 전 조회 1,048
15년 전 조회 1,862
15년 전 조회 823
15년 전 조회 974
15년 전 조회 978
15년 전 조회 990
15년 전 조회 1,729
15년 전 조회 3,034
15년 전 조회 2,119
15년 전 조회 2,019
15년 전 조회 1,015
15년 전 조회 1,112
15년 전 조회 1,066
15년 전 조회 949
15년 전 조회 1,233
15년 전 조회 949
15년 전 조회 1,394
15년 전 조회 841
15년 전 조회 749
15년 전 조회 1,627
15년 전 조회 890
15년 전 조회 1,330
15년 전 조회 1,198
15년 전 조회 877
15년 전 조회 899
15년 전 조회 808
15년 전 조회 784
15년 전 조회 950
15년 전 조회 1,198
15년 전 조회 2,371
15년 전 조회 970
15년 전 조회 1,053
15년 전 조회 1,094
15년 전 조회 1,090
15년 전 조회 1,159
15년 전 조회 773
15년 전 조회 2,029
15년 전 조회 875
15년 전 조회 1,680
15년 전 조회 825
15년 전 조회 840
15년 전 조회 1,505
15년 전 조회 1,118
15년 전 조회 1,412
15년 전 조회 1,727
15년 전 조회 1,353
15년 전 조회 1,453
15년 전 조회 815
15년 전 조회 2,363
15년 전 조회 1,090
15년 전 조회 827
15년 전 조회 1,176
15년 전 조회 885
15년 전 조회 2,147
15년 전 조회 1,174
15년 전 조회 990