Search code examples
phpsplitexplode

Spliting content by not so clear marker - PHP preg_spllit or explode


I have this string (file content) with sections:

0
3
1
Ref-1
5-18-100 Xeeecooo



-1

0
11
1
FK-1567
88-158-62 Maxco




-1

0
5
.....

that always are same when number of rows is considered.

I need to break those sections (so I can tweek the internal lines of each) and the marker here is -1 After bunch of tries with explode and preg_match I am still nowhere.

eg. preg_split( "/^-1$/m", $file_content);

Is there a way to achieve what I've been trying to achieve?


Solution

  • You can allow an arbitrary number of whitespace characters (but at least one) with \s+ (space, new lines, ...) before and after the -1 with this expression :

    $res = preg_split( "/\s+-1\s+/", $file_content);