리자

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

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,715
15년 전 조회 2,069
15년 전 조회 1,494
15년 전 조회 7,692
15년 전 조회 1,128
15년 전 조회 1,343
15년 전 조회 1,067
15년 전 조회 1,291
15년 전 조회 1,057
15년 전 조회 1,272
15년 전 조회 991
15년 전 조회 2,100
15년 전 조회 1,112
15년 전 조회 1,013
15년 전 조회 1,505
15년 전 조회 1,393
15년 전 조회 1,128
15년 전 조회 993
15년 전 조회 1,032
15년 전 조회 1,335
15년 전 조회 895
15년 전 조회 974
15년 전 조회 1,164
15년 전 조회 1,866
15년 전 조회 1,185
15년 전 조회 1,271
15년 전 조회 1,054
15년 전 조회 983
15년 전 조회 1,364
15년 전 조회 1,814
15년 전 조회 1,099
15년 전 조회 1,433
15년 전 조회 1,703
15년 전 조회 1,062
15년 전 조회 986
15년 전 조회 1,141
15년 전 조회 1,210
15년 전 조회 1,015
15년 전 조회 1,293
15년 전 조회 1,375
15년 전 조회 1,295
15년 전 조회 3,210
15년 전 조회 883
15년 전 조회 1,447
15년 전 조회 1,032
15년 전 조회 1,841
15년 전 조회 808
15년 전 조회 968
15년 전 조회 956
15년 전 조회 980
15년 전 조회 1,712
15년 전 조회 3,021
15년 전 조회 2,095
15년 전 조회 2,009
15년 전 조회 996
15년 전 조회 1,100
15년 전 조회 1,049
15년 전 조회 925
15년 전 조회 1,215
15년 전 조회 931
15년 전 조회 1,374
15년 전 조회 822
15년 전 조회 727
15년 전 조회 1,617
15년 전 조회 866
15년 전 조회 1,318
15년 전 조회 1,185
15년 전 조회 858
15년 전 조회 881
15년 전 조회 790
15년 전 조회 758
15년 전 조회 932
15년 전 조회 1,185
15년 전 조회 2,357
15년 전 조회 947
15년 전 조회 1,035
15년 전 조회 1,076
15년 전 조회 1,066
15년 전 조회 1,147
15년 전 조회 751
15년 전 조회 2,012
15년 전 조회 858
15년 전 조회 1,668
15년 전 조회 813
15년 전 조회 821
15년 전 조회 1,493
15년 전 조회 1,100
15년 전 조회 1,389
15년 전 조회 1,707
15년 전 조회 1,342
15년 전 조회 1,434
15년 전 조회 802
15년 전 조회 2,353
15년 전 조회 1,079
15년 전 조회 813
15년 전 조회 1,160
15년 전 조회 871
15년 전 조회 2,129
15년 전 조회 1,162
15년 전 조회 959