2017-09-11 1 views
0

정규식을 사용하여 특정 URL로 링크를 추출하려고했지만 실패했습니다. 아래 정규식을 사용하여 PHP를 사용하여 링크를 추출하려고 시도했습니다.Regex 특정 경로로 HTML에서 링크를 추출하는 방법

preg_match_all('/\\<a href="(.*?)\\">/', $data1, $matches); 

와 HTML 여기에 전체 HTML 나는이 링크를 필요로의 링크가 많이

<a href="https://www.website.com/n/?confirm.php" ></a> 

스 니펫이다.

+0

이 한 번 봐 여기에서 찾을 수 : [XHTML 자체 포함 된 태그를 제외하고 정규식 일치 열린 태그 (https://stackoverflow.com/questions/1732348/regex-match-open- tags-except-xhtml-self-contained-tags/1732454 # 1732454) 및 이것 : [The DOMDocument class] (http://php.net/manual/de/class.domdocument.php) – insertusernamehere

+0

모든 URL 추출 DOM 인 경우),'preg_grep'을 시도해 * 특정 부분 *을 포함하는 것을 출력하십시오. – revo

+0

@revo 당신의 방식에 따라 어떤 대답입니까? –

답변

0

귀하의 질문에 오해가 없으면이 방법이 효과적입니다.

$html = '<a href="https://www.website.com/n/?confirm.php" ></a>'; 
preg_match_all('/href="([^\s"]+)/', $html, $match); 
print '<pre>'; 
print_r($match); 
print '</pre>'; 
print $match[1][0]; 

편집는 : 코멘트 당, 당신은 우리에게 그냥 href를 캡처하는 일반적인 대답을 게시 왜 특정 URL을 제공하지 않았다. 내 아래 답변을 참조하십시오. 중고 정규식 https://regex101.com/r/pnfz7E/1

$re = '/<a href="([^"]*?\/n\/\?confirm\.php)">.*?<\/a>/m'; 
$str = '<a href="https://www.website.com/n/?noconfirm.php">SSD</a> 
<div>How are you</div> 
<a href="https://www.website.com/n/?confirm.php">HDD</a> 
<h2>Being Sunny</h2> 
<a href="https://www.ltmgtfu.com/n/?noconfirm.php">MSD</a> 
<div>How are you</div> 
<a href="https://www.website.com/n/?confirm.php"></a> 
<h2>Being Sunny</h2> 
<a href="https://www.google.com/n/?noconfirm.php">GSD</a> 
<div>How are you</div> 
<a href="https://www.website.com/n/?confirm.php">LSD</a> 
<h2>Being Sunny</h2>'; 

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0); 

// Print the entire match result 
print '<pre>'; 
print_r($matches); 
print '</pre>'; 
+0

모든 앵커 태그가 인쇄됩니다. 하나 /n/?confirm.php 수 좀더 구체적으로. –

+0

@OwaisIqbal이 수정 된 답변을 확인했습니다. 나는 그것이 당신을 위해 일하기를 바랍니다. –

관련 문제