2013-07-22 6 views
1
<a href="http://newday.com/song.mp3">First Link</a> 
<div id="right_song"> 
     <div style="font-size:15px;"><b>Pitbull ft. Chris Brown - Pitbull feat. Chris Brown - International Love mp3</b></div> 
     <div style="clear:both;"></div> 
<div style="float:left;"> 
    <div style="float:left; height:27px; font-size:13px; padding-top:2px;"> 
     <div style="float:left;"> 
    <a href="http://secondurl.com/thisoneshouldonlyoutput" rel="nofollow" target="_blank" style="color:green;">Second Link</a></div>'; 

pregmatch_all을 사용하여이 HTML에서 두 번째 링크를 가져오고 싶습니다. 나의 현재 정규식은 다음과 같습니다URL을 찾는 정규식

preg_match_all("/\<a.+?href=(\"|')(?!javascript:|#)(.+?)\.mp3(\"|')/i", $html, $urlMatches); 

이 잘 작동 나는 두 개의 링크 출력을 얻을 수 있지만, 나는 단지 두 번째는 .MP3 확장자없이 출력되고 싶어요. 나에게

+0

우리는 HTML DOM 파서 정규식 – DevZer0

+0

구문 분석 HTML DOM과 HTML을 구문 분석하지 않습니다 - http://simplehtmldom.sourceforge.net/ – DivinusVox

+0

내가 정규식을 사용하지 않도록 알고 그러나 나는 잠시 동안해야한다. 어떤 도움이 필요합니까? –

답변

0

설명을 도와주세요

이 정규식 것이다

  • 일치하는 값을 구성하는 에지 많은 경우를 피할 수 .mp3
  • 으로 끝나는 href 속성을 가지고 <div id="rigth_song"> 후 첫 앵커 태그 html 텍스트를 정규 표현식과 매치시키는 것은 매우 어렵습니다.

<div\sid="right_song">.*?<a(?=\s|>)(?=(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*?\shref=(['"]?)(.*?\.mp3)\1(?:\s|\/>|>))(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*>.*?<\/a>

enter image description here

샘플 텍스트

참고 href="bad.mp3"이 속성 값에 중첩되는 문자열과 같은 제 앵커 태그 어려운 가장자리 케이스; 거기에 더 큰 자바 스크립트가 > 값 안에있다; 실제 href 속성은 인용 부호가 없습니다.

<a href="http://newday.com/song.mp3">First Link</a> 
<div id="right_song"> 
     <div style="font-size:15px;"><b>Pitbull ft. Chris Brown - Pitbull feat. Chris Brown - International Love mp3</b></div> 
     <div style="clear:both;"></div> 
<div style="float:left;"> 
    <div style="float:left; height:27px; font-size:13px; padding-top:2px;"> 
     <div style="float:left;"> 
<a onmouseover=' href="bad.mp3" ; if (6 > x) {funRotate(href); } ; ' href="http://secondurl.com/thisoneshouldonlyoutput.mp3">First Link</a> 
</div> 

코드

<?php 
$sourcestring="your source string"; 
preg_match('/<div\sid="right_song">.*?<a(?=\s|>)(?=(?:[^>=]|=\'[^\']*\'|="[^"]*"|=[^\'"][^\s>]*)*?\shref=([\'"]?)(.*?\.mp3)\1(?:\s|\/>|>))(?:[^>=]|=\'[^\']*\'|="[^"]*"|=[^\'"][^\s>]*)*>.*?<\/a> 
/imsx',$sourcestring,$matches); 
echo "<pre>".print_r($matches,true); 
?> 

매치 그룹 0은 <div에서 텍스트를 가져

통해
그룹 (1) 주위의 개방 견적을 얻는 완전 일치하는 앵커 태그를 포함한으로
나중에 참조되는 href 값
그룹 2 HREF 값을 가져옵니다

[0] => <div id="right_song"> 
     <div style="font-size:15px;"><b>Pitbull ft. Chris Brown - Pitbull feat. Chris Brown - International Love mp3</b></div> 
     <div style="clear:both;"></div> 
<div style="float:left;"> 
    <div style="float:left; height:27px; font-size:13px; padding-top:2px;"> 
     <div style="float:left;"> 
<a onmouseover=' href="bad.mp3" ; if (6 > x) {funRotate(href); } ; ' href="http://secondurl.com/thisoneshouldonlyoutput.mp3">First Link</a> 
[1] => " 
[2] => http://secondurl.com/thisoneshouldonlyoutput.mp3 
+0

div ID 확인 또는 div 스타일 확인을 추가 할 수 없습니까? –

+0

샘플 텍스트에 열기 및 닫기 div 태그가 없습니다. 정규식이 HTML 문서 내에서 일치하는 문자열로 할 수있는 한계에 관한 것입니다. 중첩 된 div 태그를 매치 시키면 문제가 더욱 복잡해집니다. 페이지에서 두 번째 일치라고 판단되면 두 번째 일치를 취하는 것이 가장 좋을 것입니다. 버그가있는 정규 표현식을 만들거나 구문 분석 도구를 사용하는 것이 좋습니다. –

+0

html 코드가 인터넷에서로드되고 있으며 코딩 방법을 제어 할 수 없습니다. 고마워 어쨌든 –