리자

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

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,638
15년 전 조회 1,995
15년 전 조회 1,417
15년 전 조회 7,614
15년 전 조회 1,045
15년 전 조회 1,269
15년 전 조회 995
15년 전 조회 1,208
15년 전 조회 986
15년 전 조회 1,200
15년 전 조회 927
15년 전 조회 2,021
15년 전 조회 1,037
15년 전 조회 945
15년 전 조회 1,428
15년 전 조회 1,321
15년 전 조회 1,042
15년 전 조회 925
15년 전 조회 960
15년 전 조회 1,245
15년 전 조회 821
15년 전 조회 894
15년 전 조회 1,097
15년 전 조회 1,792
15년 전 조회 1,104
15년 전 조회 1,195
15년 전 조회 977
15년 전 조회 909
15년 전 조회 1,284
15년 전 조회 1,743
15년 전 조회 1,016
15년 전 조회 1,355
15년 전 조회 1,630
15년 전 조회 980
15년 전 조회 915
15년 전 조회 1,069
15년 전 조회 1,123
15년 전 조회 938
15년 전 조회 1,214
15년 전 조회 1,293
15년 전 조회 1,220
15년 전 조회 3,144
15년 전 조회 823
15년 전 조회 1,372
15년 전 조회 953
15년 전 조회 1,773
15년 전 조회 732
15년 전 조회 886
15년 전 조회 900
15년 전 조회 908
15년 전 조회 1,642
15년 전 조회 2,942
15년 전 조회 2,027
15년 전 조회 1,934
15년 전 조회 928
15년 전 조회 1,028
15년 전 조회 978
15년 전 조회 862
15년 전 조회 1,151
15년 전 조회 852
15년 전 조회 1,317
15년 전 조회 749
15년 전 조회 659
15년 전 조회 1,546
15년 전 조회 807
15년 전 조회 1,245
15년 전 조회 1,108
15년 전 조회 791
15년 전 조회 818
15년 전 조회 721
15년 전 조회 701
15년 전 조회 865
15년 전 조회 1,110
15년 전 조회 2,285
15년 전 조회 884
15년 전 조회 971
15년 전 조회 1,000
15년 전 조회 998
15년 전 조회 1,063
15년 전 조회 693
15년 전 조회 1,940
15년 전 조회 781
15년 전 조회 1,587
15년 전 조회 744
15년 전 조회 752
15년 전 조회 1,419
15년 전 조회 1,038
15년 전 조회 1,326
15년 전 조회 1,633
15년 전 조회 1,270
15년 전 조회 1,366
15년 전 조회 727
15년 전 조회 2,280
15년 전 조회 1,015
15년 전 조회 751
15년 전 조회 1,095
15년 전 조회 799
15년 전 조회 2,054
15년 전 조회 1,089
15년 전 조회 907
🐛 버그신고