웹 페이지 소스에서 문자열을 가져 오는 방법은 무엇입니까? 나는 PHP.net 전체를 살펴 보았고 PHP에 문자열을 가져올 수있는 함수 또는 함수 집합이 있는지 확인할 수 없었습니다.
예를 들어, 다음은에 저장된 웹 페이지에서 현재 가지고있는 것입니다 (에서 "wgCategories"
~ "wgMonthNamesShort"
까지 모든 것을 가져오고 싶습니다 ) $html
.
<?php
error_reporting(E_ALL);
$html = file_get_contents('http://en.wikipedia.org/wiki/Los_Angeles');
$string = <>;
?>
먼저 웹 페이지의 소스를 $ html 변수로 가져 왔습니다. 지금은 모든 것을 잡을 수있는 함수의 기능이나 설정이 필요 "wgCategories"
로 "wgMonthNamesShort"
하고 $ 문자열로 저장합니다.
원하는 결과 :
$string = "wgCategories":["All articles with dead external links","Articles with dead external links from March 2013","Articles with dead external links from March 2014","Pages with broken reference names","Articles with dead external links from January 2014","Articles with dead external links from September 2011","Articles with dead external links from October 2011","CS1 errors: dates","Use mdy dates from May 2014","Wikipedia indefinitely semi-protected pages","Wikipedia indefinitely move-protected pages","Coordinates on Wikidata","Articles including recorded pronunciations","Articles containing Spanish-language text","All articles with unsourced statements","Articles with unsourced statements from December 2013","Spoken articles","Articles with hAudio microformats","Los Angeles, California","Cities in Los Angeles County, California","Communities on U.S. Route 66","County seats in California","Incorporated cities and towns in California","Populated coastal places in California","Populated places established in 1781","Port cities and towns of the United States Pacific coast","Butterfield Overland Mail in California","Stockton - Los Angeles Road"],"wgBreakFrames":false,"wgPageContentLanguage":"en","wgPageContentModel":"wikitext","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"dmy","wgMonthNames":["","January","February","March","April","May","June","July","August","September","October","November","December"],"wgMonthNamesShort";
마지막으로에서부터 "wgCategories"
까지의 모든 것이 태그 "wgMonthNamesShort"
사이에 저장 된다는 점에 유의 <script>
하십시오 (중요한지는 확실하지 않지만 누군가 언급 할 가치가 있다고 말했습니다).
설명이 필요하면 알려주세요.
플래그 (DOTALL) preg_match
와 함께 사용 하여 s
두 키워드 사이의 문자열을 가져올 수 있습니다.
error_reporting(E_ALL);
$html = file_get_contents('http://en.wikipedia.org/wiki/Los_Angeles');
if (preg_match('/wgCategories.*?wgMonthNamesShort/is', $html, $matches))
echo $matches[0];
정규식을 피하고 stristr
.
위의 코드는 다음을 인쇄합니다.
wgCategories":["All articles with dead external links","Articles with dead external links from March 2013","Articles with dead external links from March 2014","Pages with broken reference names","Articles with dead external links from January 2014","Articles with dead external links from September 2011","Articles with dead external links from October 2011","CS1 errors: dates","Use mdy dates from May 2014","Wikipedia indefinitely semi-protected pages","Wikipedia indefinitely move-protected pages","Coordinates on Wikidata","Articles including recorded pronunciations","Articles containing Spanish-language text","All articles with unsourced statements","Articles with unsourced statements from December 2013","Spoken articles","Articles with hAudio microformats","Los Angeles, California","Cities in Los Angeles County, California","Communities on U.S. Route 66","County seats in California","Incorporated cities and towns in California","Populated coastal places in California","Populated places established in 1781","Port cities and towns of the United States Pacific coast","Butterfield Overland Mail in California","Stockton - Los Angeles Road"],"wgBreakFrames":false,"wgPageContentLanguage":"en","wgPageContentModel":"wikitext","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"dmy","wgMonthNames":["","January","February","March","April","May","June","July","August","September","October","November","December"],"wgMonthNamesShort
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다