리자

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

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,623
15년 전 조회 1,978
15년 전 조회 1,402
15년 전 조회 7,600
15년 전 조회 1,033
15년 전 조회 1,254
15년 전 조회 979
15년 전 조회 1,194
15년 전 조회 971
15년 전 조회 1,187
15년 전 조회 909
15년 전 조회 2,006
15년 전 조회 1,020
15년 전 조회 927
15년 전 조회 1,413
15년 전 조회 1,306
15년 전 조회 1,026
15년 전 조회 909
15년 전 조회 941
15년 전 조회 1,230
15년 전 조회 809
15년 전 조회 880
15년 전 조회 1,079
15년 전 조회 1,772
15년 전 조회 1,086
15년 전 조회 1,179
15년 전 조회 959
15년 전 조회 895
15년 전 조회 1,266
15년 전 조회 1,729
15년 전 조회 1,002
15년 전 조회 1,340
15년 전 조회 1,618
15년 전 조회 966
15년 전 조회 902
15년 전 조회 1,053
15년 전 조회 1,103
15년 전 조회 922
15년 전 조회 1,197
15년 전 조회 1,275
15년 전 조회 1,210
15년 전 조회 3,127
15년 전 조회 808
15년 전 조회 1,356
15년 전 조회 937
15년 전 조회 1,755
15년 전 조회 716
15년 전 조회 872
15년 전 조회 885
15년 전 조회 889
15년 전 조회 1,625
15년 전 조회 2,929
15년 전 조회 2,008
15년 전 조회 1,922
15년 전 조회 913
15년 전 조회 1,017
15년 전 조회 962
15년 전 조회 846
15년 전 조회 1,135
15년 전 조회 831
15년 전 조회 1,304
15년 전 조회 734
15년 전 조회 645
15년 전 조회 1,532
15년 전 조회 791
15년 전 조회 1,229
15년 전 조회 1,094
15년 전 조회 776
15년 전 조회 803
15년 전 조회 707
15년 전 조회 687
15년 전 조회 850
15년 전 조회 1,098
15년 전 조회 2,270
15년 전 조회 870
15년 전 조회 956
15년 전 조회 983
15년 전 조회 985
15년 전 조회 1,052
15년 전 조회 675
15년 전 조회 1,924
15년 전 조회 764
15년 전 조회 1,574
15년 전 조회 731
15년 전 조회 738
15년 전 조회 1,404
15년 전 조회 1,024
15년 전 조회 1,310
15년 전 조회 1,612
15년 전 조회 1,254
15년 전 조회 1,345
15년 전 조회 712
15년 전 조회 2,261
15년 전 조회 1,002
15년 전 조회 738
15년 전 조회 1,079
15년 전 조회 785
15년 전 조회 2,035
15년 전 조회 1,076
15년 전 조회 893
🐛 버그신고