소개
PHP에서 HTML 코드를 쉽게 구문 분석하는 방법은 아마도 모든 PHPer가 직면하게 될 문제일 것입니다. phpQuery를 사용하면 jQuery만큼 편리하게 PHP 프로세스 HTML 코드를 만들 수 있습니다.
프로젝트 주소: https://code.google.com/p/phpquery/
github 주소: https://github.com/TobiaszCudnik/phpquery
데모
라이브러리 파일 다운로드: https://code.google.com/p/phpquery/downloads/list
원파일 버전을 다운로드했습니다: phpQuery-0.9.5.386-onefile.zip
공식 데모: https://code.google.com/p/phpquery/source/browse/branches/dev/demo.php
그런 다음 프로젝트에 참고하세요.
html 파일 test.html:
<span><</span><span>div </span><span>class</span><span>="thumb"</span><span> id</span><span>="Thumb-13164-3640"</span><span> style</span><span>="position: absolute; left: 0px; top: 0px;"</span><span>></span> <span><</span><span>a </span><span>href</span><span>="/Spiderman-City-Drive"</span><span>></span> <span><</span><span>img </span><span>src</span><span>="/thumb/12/Spiderman-City-Drive.jpg"</span><span> alt</span><span>=""</span><span>></span> <span><</span><span>span </span><span>class</span><span>="GameName"</span><span> id</span><span>="GameName-13164-3640"</span><span> style</span><span>="display: none;"</span><span>></span>Spiderman City Drive<span></</span><span>span</span><span>></span> <span><</span><span>span </span><span>class</span><span>="GameRating"</span><span> id</span><span>="GameRating-13164-3640"</span><span> style</span><span>="display: none;"</span><span>></span> <span><</span><span>span </span><span>style</span><span>="width: 68.14816px;"</span><span>></</span><span>span</span><span>></span> <span></</span><span>span</span><span>></span> <span></</span><span>a</span><span>></span> <span></</span><span>div</span><span>></span> <span><</span><span>div </span><span>class</span><span>="thumb"</span><span> id</span><span>="Thumb-13169-5946"</span><span> style</span><span>="position: absolute; left: 190px; top: 0px;"</span><span>></span> <span><</span><span>a </span><span>href</span><span>="/Spiderman-City-Raid"</span><span>></span> <span><</span><span>img </span><span>src</span><span>="/thumb/12/Spiderman-City-Raid.jpg"</span><span> alt</span><span>=""</span><span>></span> <span><</span><span>span </span><span>class</span><span>="GameName"</span><span> id</span><span>="GameName-13169-5946"</span><span> style</span><span>="display: none;"</span><span>></span>Spiderman - City Raid<span></</span><span>span</span><span>></span> <span><</span><span>span </span><span>class</span><span>="GameRating"</span><span> id</span><span>="GameRating-13169-5946"</span><span> style</span><span>="display: none;"</span><span>></span> <span><</span><span>span </span><span>style</span><span>="width: 67.01152px;"</span><span>></</span><span>span</span><span>></span> <span></</span><span>span</span><span>></span> <span></</span><span>a</span><span>></span> <span></</span><span>div</span><span>></span>
php 처리:
<?<span>php </span><span>include</span>('phpQuery-onefile.php'<span>); </span><span>$filePath</span> = 'test.html'<span>; </span><span>$fileContent</span> = <span>file_get_contents</span>(<span>$filePath</span><span>); </span><span>$doc</span> = phpQuery::newDocumentHTML(<span>$fileContent</span><span>); phpQuery</span>::selectDocument(<span>$doc</span><span>); </span><span>$data</span> = <span>array</span><span>( </span>'name' => <span>array</span>(), 'href' => <span>array</span>(), 'img' => <span>array</span><span>() ); </span><span>foreach</span> (pq('a') <span>as</span> <span>$t</span><span>) { </span><span>$href</span> = <span>$t</span> -> getAttribute('href'<span>); </span><span>$data</span>['href'][] = <span>$href</span><span>; } </span><span>foreach</span> (pq('img') <span>as</span> <span>$img</span><span>) { </span><span>$data</span>['img'][] = <span>$domain</span> . <span>$img</span> -> getAttribute('src'<span>); } </span><span>foreach</span> (pq('.GameName') <span>as</span> <span>$name</span><span>) { </span><span>$data</span>['name'][] = <span>$name</span> -><span> nodeValue; } </span><span>var_dump</span>(<span>$data</span><span>); </span>?>
위 코드에는 속성 및 innerText 콘텐츠 가져오기(nodeValue를 통해 가져오기)가 포함되어 있습니다.
출력:
<span>array</span> (size=3<span>) </span>'name' => <span>array</span> (size=2<span>) </span>0 => <span>string</span> 'Spiderman City Drive' (length=20<span>) </span>1 => <span>string</span> 'Spiderman - City Raid' (length=21<span>) </span>'href' => <span>array</span> (size=2<span>) </span>0 => <span>string</span> 'http://www.gahe.com/Spiderman-City-Drive' (length=40<span>) </span>1 => <span>string</span> 'http://www.gahe.com/Spiderman-City-Raid' (length=39<span>) </span>'img' => <span>array</span> (size=2<span>) </span>0 => <span>string</span> 'http://www.gahe.com/thumb/12/Spiderman-City-Drive.jpg' (length=53<span>) </span>1 => <span>string</span> 'http://www.gahe.com/thumb/12/Spiderman-City-Raid.jpg' (length=52)
가장 강력한 것은 pq 선택기입니다. 구문은 jQuery와 유사하여 매우 편리합니다.
위의 내용을 포함하여 phpQuery를 사용하여 jquery와 같은 html 코드를 구문 분석하는 방법을 소개합니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.