PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Dec 14, 2017 11:44 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Sat May 06, 2017 2:12 pm 
Offline
Forum Newbie

Joined: Sat May 06, 2017 1:37 pm
Posts: 2
I've been stuck on this for hours so I'm finally asking for help. I'm not sure if I'm going about this correctly but I have a text file that looks something like this:

Syntax: [ Download ] [ Hide ]

[Beginning]
ABC
[Ending]

[Beginning]
DEF
[Ending]


[Beginning]
GHI
[Ending]


[Beginning]
JKL
[Ending]

[Beginning]
MNO
[Ending]

 


You get the point. I've been trying to use strpos, substr, looked at preg_match but I am just really confused and lost at this point.

I want to be able to create 2 different arrays from this data.

One array like this:

Syntax: [ Download ] [ Hide ]

$array1 = array(
         "ABC",
         "DEF",
         "GHI",
          "JKL",
          "MNO"
);
 


And another array like this:

Syntax: [ Download ] [ Hide ]

$array2 = array(

"[Beginning]
ABC
[Ending]"
,


"[Beginning]
DEF
[Ending]"
,


"[Beginning]
GHI
[Ending]"
,


"[Beginning]
JKL
[Ending]"
,

"[Beginning]
MNO
[Ending]"


);

 


This way I can use foreach loops to process these arrays as needed.


Top
 Profile  
 
PostPosted: Sat May 06, 2017 2:23 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6605
Location: WA, USA
Syntax: [ Download ] [ Hide ]
preg_match_all('/^' . preg_quote('[Beginning]') . '[\r\n]+(.*)[\r\n]+' . preg_quote('[Ending]') . '$/m', $string, $matches)

can give you exactly that.


Top
 Profile  
 
PostPosted: Sat May 06, 2017 5:21 pm 
Offline
Forum Newbie

Joined: Sat May 06, 2017 1:37 pm
Posts: 2
Thanks this worked fine on the link you posted but when I run the code on my server it only returns the last set of data and doesn't give me any of the other results.


Top
 Profile  
 
PostPosted: Sat May 06, 2017 7:37 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13458
Location: New York, NY, US
If you want to generate both arrays in one pass, you may want read file into an array (readfile() I think) and then loop through looking for "[Beginning]" and "[Ending]". You'll need a counter for the arrays you are writing to and another var to track state.

_________________
(#10850)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 22 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group