리자

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

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,899
15년 전 조회 2,266
15년 전 조회 1,690
15년 전 조회 7,901
15년 전 조회 1,332
15년 전 조회 1,552
15년 전 조회 1,280
15년 전 조회 1,503
15년 전 조회 1,250
15년 전 조회 1,473
15년 전 조회 1,185
15년 전 조회 2,290
15년 전 조회 1,303
15년 전 조회 1,211
15년 전 조회 1,708
15년 전 조회 1,596
15년 전 조회 1,324
15년 전 조회 1,182
15년 전 조회 1,235
15년 전 조회 1,524
15년 전 조회 1,099
15년 전 조회 1,172
15년 전 조회 1,361
15년 전 조회 2,072
15년 전 조회 1,339
15년 전 조회 1,452
15년 전 조회 1,215
15년 전 조회 1,146
15년 전 조회 1,528
15년 전 조회 1,981
15년 전 조회 1,261
15년 전 조회 1,592
15년 전 조회 1,868
15년 전 조회 1,245
15년 전 조회 1,151
15년 전 조회 1,316
15년 전 조회 1,364
15년 전 조회 1,170
15년 전 조회 1,455
15년 전 조회 1,543
15년 전 조회 1,457
15년 전 조회 3,383
15년 전 조회 1,027
15년 전 조회 1,602
15년 전 조회 1,193
15년 전 조회 2,001
15년 전 조회 968
15년 전 조회 1,110
15년 전 조회 1,115
15년 전 조회 1,137
15년 전 조회 1,863
15년 전 조회 3,191
15년 전 조회 2,268
15년 전 조회 2,160
15년 전 조회 1,140
15년 전 조회 1,264
15년 전 조회 1,191
15년 전 조회 1,079
15년 전 조회 1,375
15년 전 조회 1,084
15년 전 조회 1,531
15년 전 조회 963
15년 전 조회 875
15년 전 조회 1,757
15년 전 조회 1,019
15년 전 조회 1,458
15년 전 조회 1,326
15년 전 조회 997
15년 전 조회 1,031
15년 전 조회 930
15년 전 조회 914
15년 전 조회 1,085
15년 전 조회 1,333
15년 전 조회 2,511
15년 전 조회 1,099
15년 전 조회 1,193
15년 전 조회 1,222
15년 전 조회 1,224
15년 전 조회 1,294
15년 전 조회 905
15년 전 조회 2,165
15년 전 조회 1,012
15년 전 조회 1,826
15년 전 조회 959
15년 전 조회 969
15년 전 조회 1,635
15년 전 조회 1,243
15년 전 조회 1,542
15년 전 조회 1,864
15년 전 조회 1,494
15년 전 조회 1,583
15년 전 조회 957
15년 전 조회 2,498
15년 전 조회 1,236
15년 전 조회 959
15년 전 조회 1,320
15년 전 조회 1,023
15년 전 조회 2,288
15년 전 조회 1,321
15년 전 조회 1,127