Reputation: 43
I want read a text file as C++ read it
here is example from the text file
(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)))
I want the output like
name : 256
Desc : 520
Index : 1
Image : Wea001
Specialty > aspeed : 700
Name : 257
Desc : 520
Index : 2
Image : Wea002
Speciality > Attack : 16 24
Is that possible?
I tried :
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.'';
}
But it didn't give me the correct output that I wanted.
Thank you
Upvotes: 2
Views: 147
Reputation: 711
<?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].'';
}
Assuming you have always similar data format
Upvotes: 2