2010-12-29 3 views
2

영어를 잘 못합니다. 그래서, 내가 실수 할 게면, 미안해. 내가 게임에 대한 몇 가지 정보와 사업부 상자가 사이트에RegExp PHP가 여러 개의 span 태그 사이에 텍스트 가져 오기

:

<span class="noteline">Developer:</span> 
<span class="subline">Gameloft</span> 
<span class="noteline">Genre:</span> 
<span class="subline">Racing/Arcade</span> 
<span class="noteline">Release year:</span> 
<span class="subline">2010</span> 
내가 <span class="noteline"> 사이에서 정보를 얻을 필요가

하고 닫는 것 태그 </span>

preg_match("/\<span\sclass=\"subline\"\>(.*)<\/span\>/imsU", $source, $matches); 

이 솔루션은 위의 잘 작동 그러나 "gameloft"라는 텍스트를 가진 "subline"만 얻습니다.

하지만 텍스트가있는 레이 라인/아케이드 및 2010;

어쩌면 이렇게 될 수 있습니다 (작동하지 않습니다). 이 같은

for developer = preg_match("/*(\<span\sclass=\"subline\"\>){1}*(.*)*(<\/span\>){1}*/imsU", $source, $matches); 
for genre = preg_match("/*(\<span\sclass=\"subline\"\>){2}*(.*)*(<\/span\>){2}*/imsU", $source, $matches); 

뭔가 .. 어쨌든

. 어떤 도움을 주셔서 감사합니다.

답변

0

이 시도 :

<?php 

$html = '<span class="noteline">Developer:</span> 
<span class="subline">Gameloft</span> 
<span class="noteline">Genre:</span> 
<span class="subline">Racing/Arcade</span> 
<span class="noteline">Release year:</span> 
<span class="subline">2010</span>'; 

preg_match_all("/<span class=\"subline\".*span>/", $html, $matches1); 

preg_match_all("/<span class=\"noteline\".*span>/", $html, $matches2); 

print_r($matches1); 
echo "<br>"; 
print_r($matches2); 

?> 

내가 가진 출력이 있었다 :

Array ([0] => Array ([0] => Gameloft [1] => Racing/Arcade [2] => 2010)) 
Array ([0] => Array ([0] => Developer: [1] => Genre: [2] => Release year:)) 
+0

고맙습니다. 그건 훌륭한 해결책 인 것 같습니다. – Andy

1

이 regexps '에 대한 대안가 될 것

preg_match_all("/<span class=\"subline\".*span>/", $html, $matches); 

preg_match_all("/<span class=\"noteline\".*span>/", $html, $matches); 

나는 위의 코드를이 방법을 시도 phpQuery 또는 QueryPath을 사용하면 간단 해집니다 :

foreach (qp($source)->find("span.subline") as $span) { 
    print $span->text(); 
} 
1

정규식은 HTML을 구문 분석하기에 적합하지 않습니다. 그들은 옳은 방향으로 가기가 어렵고 항상 가장자리에서 부서집니다.

쉬운 방법이 있는지 모르겠어요하지만 당신이 설명하는 마크 업으로 작동합니다 : 이것은 class="subline" 가정

<?php 

$fragment = '<span class="noteline">Developer:</span> 
<span class="subline">Gameloft</span> 
<span class="noteline">Genre:</span> 
<span class="subline">Racing/Arcade</span> 
<span class="noteline">Release year:</span> 
<span class="subline">2010</span>'; 

libxml_use_internal_errors(TRUE); 
$dom = new DOMDocument(); 
$dom->loadHTML($fragment); 
$xml = simplexml_import_dom($dom); 
libxml_use_internal_errors(FALSE); 

foreach($xml->xpath("//span[@class='subline']") as $item){ 
    echo (string)$item . PHP_EOL; 
} 

그래서 여러 클래스와 함께 실패 할 수 있습니다. (Xpath 입문이므로 개선을 환영합니다.)

+0

그냥 DOMDocument 클래스에 대해 알지 못했습니다. 감사합니다! – Tomatrox