리자

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

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년 전 조회 2,129
15년 전 조회 2,494
15년 전 조회 1,918
15년 전 조회 8,112
15년 전 조회 1,532
15년 전 조회 1,768
15년 전 조회 1,483
15년 전 조회 1,719
15년 전 조회 1,476
15년 전 조회 1,696
15년 전 조회 1,415
15년 전 조회 2,521
15년 전 조회 1,529
15년 전 조회 1,413
15년 전 조회 1,931
15년 전 조회 1,810
15년 전 조회 1,527
15년 전 조회 1,415
15년 전 조회 1,435
15년 전 조회 1,719
15년 전 조회 1,300
15년 전 조회 1,400
15년 전 조회 1,593
15년 전 조회 2,289
15년 전 조회 1,539
15년 전 조회 1,626
15년 전 조회 1,388
15년 전 조회 1,345
15년 전 조회 1,711
15년 전 조회 2,148
15년 전 조회 1,428
15년 전 조회 1,765
15년 전 조회 2,031
15년 전 조회 1,408
15년 전 조회 1,337
15년 전 조회 1,493
15년 전 조회 1,547
15년 전 조회 1,339
15년 전 조회 1,626
15년 전 조회 1,724
15년 전 조회 1,629
15년 전 조회 3,554
15년 전 조회 1,211
15년 전 조회 1,795
15년 전 조회 1,344
15년 전 조회 2,173
15년 전 조회 1,147
15년 전 조회 1,281
15년 전 조회 1,288
15년 전 조회 1,311
15년 전 조회 2,028
15년 전 조회 3,351
15년 전 조회 2,416
15년 전 조회 2,332
15년 전 조회 1,313
15년 전 조회 1,418
15년 전 조회 1,352
15년 전 조회 1,243
15년 전 조회 1,545
15년 전 조회 1,243
15년 전 조회 1,704
15년 전 조회 1,129
15년 전 조회 1,032
15년 전 조회 1,916
15년 전 조회 1,173
15년 전 조회 1,621
15년 전 조회 1,485
15년 전 조회 1,170
15년 전 조회 1,193
15년 전 조회 1,103
15년 전 조회 1,072
15년 전 조회 1,262
15년 전 조회 1,502
15년 전 조회 2,687
15년 전 조회 1,284
15년 전 조회 1,377
15년 전 조회 1,405
15년 전 조회 1,410
15년 전 조회 1,467
15년 전 조회 1,090
15년 전 조회 2,367
15년 전 조회 1,198
15년 전 조회 1,995
15년 전 조회 1,135
15년 전 조회 1,157
15년 전 조회 1,818
15년 전 조회 1,418
15년 전 조회 1,740
15년 전 조회 2,041
15년 전 조회 1,660
15년 전 조회 1,757
15년 전 조회 1,140
15년 전 조회 2,684
15년 전 조회 1,412
15년 전 조회 1,145
15년 전 조회 1,492
15년 전 조회 1,199
15년 전 조회 2,474
15년 전 조회 1,490
15년 전 조회 1,318