2012-12-31 2 views
1

특정 단어가있는 웹 사이트의 새 제품 페이지를 모니터링하려고합니다. 나는 이미 file_get_contents();을 사용하여 한 단어를 검색하는 기본 스크립트를 가지고 있지만 이것은 효과적이지 않습니다. 특정 단어가있는 웹 사이트에 대한 PHP 검색

은 내가 PHP에 상관없이 어떤 순서로 단어를 검색하지 않고 그들이에있는 선언을 얻으려면 어떻게해야합니까 어떻게 <table>

<td> 태그에있는 코드를 찾고 계십니까? 예 : 에서

$searchTerm = "Orange Boots"; 

:

<table> 
    <td>Boots (Red)</td> 
</table> 
<table> 
    <td>boots (ORNAGE)</td> 
</table> 
<table> 
    <td>Shirt (Green)</td> 
</table> 

는 일치를 돌려줍니다.

죄송 경우는 명확하지,하지만 난 당신이

$newcontent= (str_replace('Boots', '<span class="Red">Boots</span>',$cont)); 

같이이 작업을 수행하고 color:red;보다 붉은 색을 보여주고 싶은처럼 단지 클래스 빨간색에 대한 CSS를 쓸 수

+0

lol! 왜 당신은 클라이언트 측에서 그것을하지 않습니까? 자바 스크립트 스타일, PHP로 처리하려는 경우 ajax와 함께 보내십시오. – Alex

+4

DOM 및 Xpath 소개! http://phpmaster.com/php-dom-using-xpath/ – FredTheWebGuy

+0

http://querypath.org Querypath는 또 다른 옵션입니다. – MECU

답변

1

이해를 바랍니다 나머지

에 같은 일을하지만 더 좋은 방법은 DOM과 XPath는

1

당신이 신속하고 더러운 검색을 찾고 있다면 될 것입니다 해당 HTML 블록을 통해 preg_match_all() 함수를 사용하여 간단한 정규식을 시도 할 수 있습니다. 예를 들어, 당신은 시도 할 수 있습니다 : 일치하는 항목이 발견되거나되지 않은 경우

$html_block = get_file_contents(...); 
$matches_found = preg_match_all('/(orange|boots|shirt)/i', $html_block, $matches); 

$matches_found은 표시대로, 1 또는 0이 될 것입니다. $matches은 일치하는 항목으로 채워집니다.

1

컬 사용. 그것은 filegetcontents()보다 훨씬 빠릅니다. 시작 지점은 다음과 같습니다.

$target_url="http://www.w3schools.com/htmldom/dom_nodes.asp"; 
// make the cURL request to $target_url 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$target_url); 
curl_setopt($ch, CURLOPT_FAILONERROR, true); 
curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
$html= curl_exec($ch); 
if (!$html) {exit;} 
$dom = new DOMDocument(); 
@$dom->loadHTML($html); 

    $query = "(/html/body//tr)"; //this is where the search takes place 

$xpath = new DOMXPath($dom); 
$result = $xpath->query($query); 

for ($i = 0; $i <$result->length; $i++) { 
    $node = $result->item(0); 
    echo "{$node->nodeName} - {$node->nodeValue}<br />"; 
} 
관련 문제