-1

I have a string that I'm trying to split into smaller strings at d+\/ (any number followed by a /). Here is my string:

2/  This is a string 
that has some words on a new line
3/ This is another string that might also have some words on a new line

So far i'm using this code:

$re = '/\d+\/\s+.*/';
$str = '2/  This is a string 
that has some words on a new line
3/ This is another string that might also have some words on a new line';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

But the problem is that for the first match I'm getting only the first line:

[Match 1] 2/  This is a string
[Match 2] 3/ This is another string that might also have some words on a new line

How can I make every match to get all the lines up until d+\/ or up until the end of the full string/text?

Desired result:

[Match 1] 2/  This is a string that has some words on a new line
[Match 2] 3/ This is another string that might also have some words on a new line
Melody
  • 127
  • 10
  • just cause you say split , do it mean use split php funcshun, yes ? yuo unspire someone to dupr queshun. if that what you need i will delete answer cause i dont recomend –  Jun 14 '20 at 18:42

1 Answers1

0

split with '~(?=\d+/)~'. whateveer funct use preg_split mebe.

update: Not advisibal to use split to extract records
as the first element won't be a record, will be junk that has to be tranposed.
Also split using just assertion is so slow it like watching paint dry.
Whoever marked this as a duplicate of split to get records is juster plane wrong!
If following that logic, thousands of questions are dups of split and its
time for SO administers to edit their DB, or are we SELECTIVE without reason ?

The recommended / preferred / sensible way is to strictly match all
records with this (?s)\d+/.*?(?=\d+/|$)

demo

Demo contains comments on funcshun of regeex.

  • hey @Edward, both are working, but can you please explain at least the second pattern a little bit? Thank you! :D – Melody Jun 12 '20 at 22:38
  • `(?s)` is the same as `/your-regex/s` -> its a flag also called `modifier`. `s` stands for `single line`, means a dot matches newline – ChrisG Jun 13 '20 at 00:53
  • updaterd demo link contaner commuints on funshun –  Jun 14 '20 at 18:39