2012-09-11 5 views
2

나는 C가 여기이 텍스트 파일의 내용을 읽고 구문 분석하려면 어떻게해야합니까?

(item(name 256) (desc 520)(Index 1)(Image "Wea001") (specialty (aspeed 700))) 
(item (name 257) (desc 520)   (Index 2) (Image "Wea002")(specialty(Attack 16 24))) 

내가 원하는 텍스트 파일의 예입니다 읽어 ++과 같은 텍스트 파일을 읽고 싶어

name : 256 
Desc : 520 
Index : 1 
Image : Wea001 
Specialty > aspeed : 700 

Name : 257 
Desc : 520 
Index : 2 
Image : Wea002 
Speciality > Attack : 16 24 

같은 출력

그게 가능합니까?

내가 시도 :

preg_match_all('/name\s+(.*?)\)\+\(desc\s+(.*?)\)\+\(Index\s+(.*?)\)/', $text, $matches, PREG_SET_ORDER); 

foreach ($matches as $match) { 
    list (, $name, $desc, $index) = $match; 
    echo 'name : '.$name.' <br> Desc : '.$desc.'<br> Index : '.$index.''; 
    } 

그러나 그것은 나에게 내가 원하는 정확한 출력을 제공하지 않았다.

+3

신성한 소를 가정하면 감사드립니다. 이것은 어디서 오는 거니? 그것을 바꿀 수 있습니까? – DaveRandom

+0

Lisp 공격! 너무 많은 괄호! – Tchoupi

답변

2
<?php 
    $txt = '(item(name 256) (desc 520)(Index 1)(Image "Wea001") (specialty (aspeed 700))(item (name 257) (desc 520)   (Index 2) (Image "Wea002")(specialty(Attack 16 24)))'; 

    preg_match_all('/name\s+(?P<name>\w+).*desc\s+(?P<desc>\d+).*Index\s+(?P<index>\d+).*Image\s+(?P<img>.*)\).*specialty\s*\((?P<speciality>.*)\)\)\)/', $txt, $matches); 
    foreach($matches['name'] AS $id => $name){ 
     echo 'name : '.$name.' <br> Desc : '.$matches['desc'][$id].'<br> Index : '.$matches['index'][$id].'<br> speciality : '.$matches['speciality'][$id].''; 
} 

는 항상 유사한 데이터 형식에게 불쾌한 형식입니다

+0

고맙습니다. 잘 작동합니다. – DragoN

관련 문제