2010-12-08 10 views
0

PHP => 어떻게 class="font8text">N</span>'이 다음번 <span>에 'EARLL'을 줄 때와 같은 방법으로이 문자열을 검색 할 수 있습니까?이 패턴을 찾으려면 regexp

<div align="left" style=";">  
<span style="width:15px; padding:1px; border:1pt solid #999999; background-color:#CCFFCC; text-align:center;" class="font8text">Y</span> 
<span style="text-align:left; white-space:nowrap;" class="font8text">DINNIMAN</span> 
</div> 

<div align="left" style="background-color:#F8F8FF;"> 
    <span style="width:15px; padding:1px; border:1pt solid #999999; background-color:#FFCCCC; text-align:center;" class="font8text">N</span> 
    <span style="text-align:left; white-space:nowrap;" class="font8text">EARLL</span> 
</div> 
+6

는 HTML 파서를 사용하여 -> 우측 클래스와 내용으로 요소를 찾기 -> –

+0

가 이상한 다음의 연속 요소의 내용을 검색, 문제는 PHP에 대한 주장하지만입니다 태그가 추가 된 jquery. 둘 다 될 수는 없지, 그렇지? –

+0

이것을 확인하십시오 http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags –

답변

0

난 당신이 서로 간결 strpossubstr를 사용하여 더 나을 것 같아요.

예 :

$str = <insert your string here>; // populate data 
$_find = 'class="font8text">'; // set the search text 
$start = strpos($str,$find) + strlen($_find); // find the start off the text and offset by the $needle 
$len = strpos($str,'<',$start) - $start; find the end, then subtract the start for length 
$text = substr($str,$start,$len); // result 
0

이 그것을 할 것입니다 :

/class="font8text">N.*?class="font8text">(.*?)</m 

EARLL은 첫 경기 그룹에있을 것입니다. Rubular에서 사용해보십시오.

1

사용과 같은 DOM 파서 : 언급 한 바와 같이 http://simplehtmldom.sourceforge.net/

(a painless amount of times). 정규식은 HTML을 구문 분석하는 좋은 방법이 아닙니다. 사실, Regex로 HTML을 파싱 할 수는 없습니다. HTML은 어떤 형식의 일반이 아닙니다. 비트 만 추출 할 수 있습니다. 그리고 여전히 (대부분의 경우) 매우 신뢰할 수없는 데이터입니다.

DOM 파서를 사용하는 것이 좋습니다. HTML을 문서로 구문 분석하는 파서가 있기 때문에 트래버스하기가 더 쉽습니다.

예 :

include_once('simple_html_dom.php'); 

$dom = file_get_html('<html>...'); 

foreach($dom->find("div.head div.fact p.fact") as $element) 
    die($element->innertext); 
+0

+1 돔 파서 ... 대신 [댐 regx for this] (http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) –