sh44n Posted June 10, 2008 Share Posted June 10, 2008 The data file is like that Line1 Line2 Line3 Line4 <tag1> Tag1 Line1 Tag1 Line2 Tag1 Line3 Tag1 Line4 <tag2> Tag2 Line1 Tag2 Line2 Tag2 Line3 Tag2 Line4 <tag3> . . . how to regex to get the first 4 lines before the tag1 and then grabbing data between <tag1> and <tag2> and associating it with index tag1 and so on. In short data for tag1 data = betwen <tag1> and <tag2> tag2 data = betwen <tag2> and <tag3> . . . so on. Looking forward for your help guys. Quote Link to comment https://forums.phpfreaks.com/topic/109566-solved-parse-data-between-two-tags/ Share on other sites More sharing options...
Orio Posted June 10, 2008 Share Posted June 10, 2008 <?php $data = <<<DATA Line1 Line2 <tag1> Tag1 Line1 Tag1 Line2 <tag2> Tag2 Line1 Tag2 Line2 DATA; preg_match_all("/(<tag\d>)?(.+?)(<tag\d>|$)/is", $data, $result); $your_data = $result[2]; print_r($your_data); ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/109566-solved-parse-data-between-two-tags/#findComment-561993 Share on other sites More sharing options...
sh44n Posted June 10, 2008 Author Share Posted June 10, 2008 Thank you very much Orio. It works perfectly but how can I keep the <tag1> content generic without hardcoding it in regex ? Like Line1 Line2 Line3 Line4 <directors> Tag1 Line1 Tag1 Line2 Tag1 Line3 Tag1 Line4 <address> Tag2 Line1 Tag2 Line2 Tag2 Line3 Tag2 Line4 <copyright> . . . also is it possible that the $result array can contain the data as follows: $result['tag1'] = its respective data $result['tag2'] = its respective data $result['tag3'] = its respective data Quote Link to comment https://forums.phpfreaks.com/topic/109566-solved-parse-data-between-two-tags/#findComment-562002 Share on other sites More sharing options...
sh44n Posted June 10, 2008 Author Share Posted June 10, 2008 also is it possible that the $result array can contain the data as follows: $result['tag1'] = its respective data $result['tag2'] = its respective data $result['tag3'] = its respective data Quote Link to comment https://forums.phpfreaks.com/topic/109566-solved-parse-data-between-two-tags/#findComment-562006 Share on other sites More sharing options...
effigy Posted June 10, 2008 Share Posted June 10, 2008 <pre> <?php $data = <<<DATA Line1 Line2 Line3 Line4 <directors> Tag1 Line1 Tag1 Line2 Tag1 Line3 Tag1 Line4 <address> Tag2 Line1 Tag2 Line2 Tag2 Line3 Tag2 Line4 <copyright> . . . DATA; $pieces = preg_split('/^(<[^>]+>)\s*/m', $data, -1, PREG_SPLIT_DELIM_CAPTURE); $count = -1; foreach ($pieces as $piece) { ++$count; if (!preg_match('/^</', $piece)) { continue; } $piece = preg_replace('/[<>]/', '', $piece); $result[$piece] = $pieces[$count+1]; } print_r($result); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/109566-solved-parse-data-between-two-tags/#findComment-562057 Share on other sites More sharing options...
Orio Posted June 10, 2008 Share Posted June 10, 2008 That's what I manged to piece up while effigy posted his... Kinda ugly I guess, but it works fine. <?php $data = <<<DATA Line1 Line2 <directors> Tag1 Line1 Tag1 Line2 <address> Tag2 Line1 Tag2 Line2 DATA; preg_match_all("/(((\w+)>)|^)(.+?)(<|$)/is", $data, $matches); $result = array(); $result[0] = trim($matches[4][0]); //The first lines, before the first tag $num_tags = count($matches[1]) - 1; for($i = 1; $i <= $num_tags; $i++) $result[$matches[3][$i]] = trim($matches[4][$i]); echo "<pre>"; print_r($result); echo "</pre>"; ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/109566-solved-parse-data-between-two-tags/#findComment-562071 Share on other sites More sharing options...
sh44n Posted June 10, 2008 Author Share Posted June 10, 2008 Thank you all for taking time out to help. Your effort in this regard is highly appreciated. Orio, the examples works perfect but it seems to get the last index when we have data like this line1 line2 line3 <borad of directors> board1name board2name board3name <address at place> address 1 address 2 address 3 .... it seems to get in the array $result['directors'] = its respective data NOT result['board of directors'] $result['place'] = its respective data NOT result['address at place'] though fortunately, the sample data i'm working still gives a unique data to the index but still as a student currently of regex I would like to learn that how to proceed. A little explanation of the regex difference in that regard will be highly appreciated as well. Thanks in anticipation again. Quote Link to comment https://forums.phpfreaks.com/topic/109566-solved-parse-data-between-two-tags/#findComment-562307 Share on other sites More sharing options...
Orio Posted June 10, 2008 Share Posted June 10, 2008 I think this should solve the spaced keys problem. <?php $data = <<<DATA Line1 Line2 <directors> Tag1 Line1 Tag1 Line2 <address> Tag2 Line1 Tag2 Line2 DATA; preg_match_all("/((([a-z0-9_ ]+)>)|^)(.+?)(<|$)/is", $data, $matches); $result = array(); $result[0] = trim($matches[4][0]); //The first lines, before the first tag $num_tags = count($matches[1]) - 1; for($i = 1; $i <= $num_tags; $i++) $result[$matches[3][$i]] = trim($matches[4][$i]); echo "<pre>"; print_r($result); echo "</pre>"; ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/109566-solved-parse-data-between-two-tags/#findComment-562473 Share on other sites More sharing options...
sh44n Posted June 11, 2008 Author Share Posted June 11, 2008 Thanks Orio again. It works perfectly. Thumbs up.! Quote Link to comment https://forums.phpfreaks.com/topic/109566-solved-parse-data-between-two-tags/#findComment-562877 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.