당신으로, preg_match_all을 사용할 수 있습니다 HTML에있는 모든 링크를 찾으려면() :
이
는 콘텐츠를 내 코드입니다.
$links = preg_match_all ("/href=\"([^\"]+)\"/i", $content, $matches);
URL
https://qc.yahoo.com/
가 GZIP 압축을 사용하는
, 그래서 당신은 그것을 감지 기능 gzdecode를 사용하여 압축을 해제해야합니다(). (PHP 버전에 설치해야합니다)
gzip 압축은 Content-Encoding: gzip
HTTP 헤더로 표시됩니다. 헤더를 검사해야하므로 말풍선이나 비슷한 방법으로 헤더를 검색해야합니다. (file_get_contents()는 HTTP 헤더를 제공하지 않습니다 ... gzip으로 압축 된 콘텐츠 만 다운로드합니다. 압축되었지만 헤더를 읽어야 함을 감지해야합니다.)
다음은 완전한 것입니다. 예 :
<?php
$url = "https://qc.yahoo.com/";
# download resource
$c = curl_init ($url);
curl_setopt ($c, CURLOPT_HEADER, true);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec ($c);
$hsize = curl_getinfo ($c, CURLINFO_HEADER_SIZE);
curl_close ($c);
# separate headers from content
$headers = substr ($content, 0, $hsize);
$content = substr ($content, $hsize);
# check if content is compressed with gzip
$gzip = 0;
$headers = preg_split ('/\r?\n/', $headers);
foreach ($headers as $h)
{
$pieces = preg_split ("/:/", $h, 2);
$pieces2 = (count ($pieces) > 1);
$enc = $pieces2 && (preg_match ("/content-encoding/i", $pieces[0]));
$gz = $pieces2 && (preg_match ("/gzip/i", $pieces[1]));
if ($enc && $gz)
{
$gzip = 1;
break;
}
}
# unzip content if gzipped
if ($gzip)
{
$content = gzdecode ($content);
}
# find links
$links = preg_match_all ("/href=\"([^\"]+)\"/i", $content, $matches);
# output results
echo "url = " . htmlspecialchars ($url) . "<br>";
echo "links found (" . count ($matches[1]) . "):" . "<br>";
$n = 0;
foreach ($matches[1] as $link)
{
$n++;
echo "$n: " . htmlspecialchars ($link) . "<br>";
}
힘든 시간을 알고 있습니다. 샘플 '$ html'입력을 보여 주면 처리하는 데 도움이 될 것입니다. 작은 샘플. 우리가하려는 일을 충분히 이해하고 있습니다. – BeetleJuice
hy @ BeetleJuice는 http://stackoverflow.com/a/38396700/6516181에서 유감스럽게도 미안하지만 코딩 및 키워드 이름을 앞 세우지 않았습니다. 도와주세요 ^^ – ane