2012-11-15 7 views
1

PHP를 처음 사용했습니다. 아래 코드의 id1123 인 코드를 작성하고 싶습니다. 아무도 나에게 어떤 생각을 줄 수 있니?PHP를 사용하여 html 소스의 클래스 이름을 찾으십시오.

<span class="miniprofile-container /companies/1123?miniprofile=" 
     data-tracking="NUS_CMPY_FOL-nhre" 
     data-li-getjs="http://s.c.lnkd.licdn.com/scds/concat/common/js?h=dyt8o4nwtaujeutlgncuqe0dn&amp;fc=2"> 
    <strong> 
     <a href="http://www.linkedin.com/nus-trk?trkact=viewCompanyProfile&pk=biz-overview-public&pp=1&poster=&uid=5674666402166894592&ut=NUS_UNIU_FOLLOW_CMPY&r=&f=0&url=http%3A%2F%2Fwww%2Elinkedin%2Ecom%2Fcompany%2F1123%3Ftrk%3DNUS_CMPY_FOL-nhre&urlhash=7qbc"> 
     Bank of America 
     </a> 
    </strong> 
</span> has a new Project Manager 

참고 : 스팬 클래스의 콘텐츠가 필요하지 않습니다. 스팬 클래스 이름에 id이 필요합니다.

$dom = new DOMDocument('1.0', 'UTF-8'); 
@$dom->loadHTML($html); 
$xmlElements = simplexml_import_dom($dom); 
$id = $xmlElements->xpath("//span [@class='miniprofile-container /companies/$data_id?miniprofile=']"); 

...하지만 난 더 이상 진행하는 방법을 알고하지 않습니다

나는 다음을 시도했다.

당신의 필요의 의존
+0

당신이 설명해 주시겠습니까 무엇 너 지금까지 해봤 니? – Carsten

답변

1

,이 매우 사소한 정규식

$matches = array(); 
preg_match('|<span class="miniprofile-container /companies/(\d+)\?miniprofile|', $html, $matches); 
print_r($matches); 

할 수 있지만, 첫 번째 제안이 될 수 있습니다. DomDocument 또는 simplexml을 통해 이동하려는 경우 두 경우를 모두 섞어서는 안됩니다. 선호하는 방법은 무엇입니까?이를 좁힐 수 있습니다.

// 편집 : 꽤 말을 @fireeyedboy 많은,하지만 난 그냥 함께 바이올린을 것입니다 : 이것은 당신이 후에 무엇을해야

<?php 
$html = <<<EOD 
<html><head></head> 
<body> 
<span class="miniprofile-container /companies/1123?miniprofile=" 
     data-tracking="NUS_CMPY_FOL-nhre" 
     data-li-getjs="http://s.c.lnkd.licdn.com/scds/concat/common/js?h=dyt8o4nwtaujeutlgncuqe0dn&amp;fc=2"> 
    <strong> 
     <a href="#"> 
     Bank of America 
     </a> 
    </strong> 
</span> has a new Project Manager 

</body> 
</html> 
EOD; 

$domDocument = new DOMDocument('1.0', 'UTF-8'); 
$domDocument->recover = TRUE; 
$domDocument->loadHTML($html); 

$xPath = new DOMXPath($domDocument); 
$relevantElements = $xPath->query('//span[contains(@class, "miniprofile-container")]'); 
$foundId = NULL; 
foreach($relevantElements as $match) { 
    $pregMatches = array(); 
    if (preg_match('|/companies/(\d+)\?miniprofile|', $match->getAttribute('class'), $pregMatches)) { 
     if (isset($pregMatches[1])) { 
      $foundId = $pregMatches[1]; 
      break; 
     } 
    }; 
} 

echo $foundId; 

?> 
+0

dom을 선호 할 것입니다 –

+0

다음 html 코드에서 id를 취하는 데 동일한 코드를 사용했지만 작동하지 않습니다 ...

1

:

$dom = new DOMDocument('1.0', 'UTF-8'); 
@$dom->loadHTML($html); 
$xpath = new DOMXPath($dom); 

/* 
* the following xpath query will find all class attributes of span elements 
* whose class attribute contain the strings " miniprofile-container " and " /companies/" 
*/ 
$nodes = $xpath->query("//span[contains(concat(' ', @class, ' '), ' miniprofile-container ') and contains(concat(' ', @class, ' '), ' /companies/')]/@class"); 
foreach($nodes as $node) 
{ 
    // extract the number found between "/companies/" and "?miniprofile" in the node's nodeValue 
    preg_match('#/companies/(\d+)\?miniprofile#', $node->nodeValue, $matches); 
    var_dump($matches[ 1 ]); 
} 
관련 문제