0

How can I convert this numbered paragraph into an array of sentences?

1. The best foods for your cat's diet
2. How to ensure your cat is getting enough nutrients
3. The importance of a balanced diet for cats
4. How to make sure your cat is getting enough water
5. Tips for dealing with finicky eaters
6. How to tell if your cat is overweight
7. The dangers of overfeeding your cat5

Desired output:

$sentences = [ 
    "The best foods for your cat's diet",
    "How to ensure your cat is getting enough nutrients",
    "The importance of a balanced diet for cats",
    "How to make sure your cat is getting enough water",
    "Tips for dealing with finicky eaters",
    "How to tell if your cat is overweight",
    "The dangers of overfeeding your cat5"
];
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
نور
  • 1,425
  • 2
  • 22
  • 38

1 Answers1

1

Split the text on:

  • optional leading newline sequences (\R* or \R?)
  • a number (\d+)
  • a dot (\.)
  • then a space ( )

Code: (Demo)

var_export(
    preg_split(
        '/\R*\d+\. /',
        $text,
        0,
        PREG_SPLIT_NO_EMPTY
    )
);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136