0

I have a string and need to split it up every 6 lines in php.

So for example the text might look something like this:

Server Name
Server IP
Server Status
Sever Temp
Server IP2
-------------------
Server Name
Server IP
Server Status
Sever Temp
Server IP2
-------------------

Note: Some of the server names and even status' might also contain a '-' character

But more than one server would appear. This always one whole string and I would like to split it into an array containing just the text for each server (I can already break down the text for each individual server)

Any ideas or help would be appreciated :)

  • 4
    Does it actually have those `-----` lines at the end of each block? Might be easier to split it on those if so. – Don't Panic Jun 13 '22 at 15:28
  • It does yes, my bad for not mentioning but some of the server names could posibly have an '-' in them – Jack Norris Jun 13 '22 at 15:32
  • 4
    It shouldn't matter if there are some `-` characters elsewhere in the string if you explode using that entire line as the delimiter (like `$servers = explode('-------------------', $string);`. – Don't Panic Jun 13 '22 at 15:36
  • I agree with @Don'tPanic, it would likely be easier to split on the hyphens. If there's always going to be a fixed number of hyphens you could split on on that exact string of hyphens which would still allow for a single '-' in the server name without getting false positives. EDIT: Don't Panic beat me to it while I was typing :) – WTFranklin Jun 13 '22 at 15:39
  • Here's another possibility: https://stackoverflow.com/a/44740226/2734189 (assuming you're going to put it into some array/object-like structure anyway) – Don't Panic Jun 13 '22 at 15:41
  • I think perhaps I am overthinking the problem (I have had a lot dumped on me with very close deadlines). Thank you all for your input, I will maybe just come back to this tomorrow with a fresh head :) – Jack Norris Jun 13 '22 at 15:44
  • Same task, different programming language: https://stackoverflow.com/q/31387486/2943403 – mickmackusa Jul 16 '22 at 14:13

0 Answers0