Adam Connah
Adam Connah

Reputation: 59

Split up a text of file using keyword to seperate blocks of text. PHP

I have a text file that is in this format:

[Term]
data
data
data

[Term]
data
data
data
data
data

[Term]
data
data
data
data
data

And would like to split the file by the word [Term], and get all the lines under it. Any ideas which commands would suit this?

Thanks

Upvotes: 1

Views: 172

Answers (1)

Nick Andriopoulos
Nick Andriopoulos

Reputation: 10643

Just use file_get_contents() to load the whole file as string, then use explode() to make it into an array based on Term. You can then explode() each element using the newline character as separator to separate each line to its own (sub)element.

If you can change your structure to key-value pairs under each Term, you might also be able to use parse-ini-file() to parse it.

Upvotes: 2

Related Questions