Q:

Break $content into array when conditions are met

I have an array with Category names $categories_array.
I have a $content variable containing a big string (product description)
My intention is to explode $content into an array ($content_array).
That is easy.
The content also contains category names on it
The problem is as follows:

category is named Kids and Baby

I cant explode by word, as I want to keep the content kids and baby in the same array entry.
I also cant query all the time as it would be resource expensive.
What is the best way to achieve something like this:
This is the original $content:
$content = "lorem ipsum bla bla bla cat1 bla bla bla kids and baby";

This is my categories array:
$categories_array = array(
    "cat1",
    "kids and baby",
    "cat3",
    ...
);

This is my expected output:
$content_array = array(
    "word1",
    "word2",
    "kids and baby",
    "word3"
);

Thank you for your guidance.

A:

For every category search content for that category and replace spaces with a character that doesnt exist in the content.
Then explode conent by spaces (so it will keep category text together).
Later just replace that character back to spaces.
<?php

$content = "lorem ipsum bla bla bla cat1 bla bla bla kids and baby";

$categories_array = array(
    "cat1",
    "kids and baby",
    "cat3",
);  

foreach($categories_array as $category) {
  $content = str_replace($category, str_replace(' ', '·', $category), $content);
}

$content_array = explode(' ', $content);

$content_array = array_map(function ($content) {return str_replace('·', ' ', $content);}, $content_array);

var_dump($content_array);

Output:
array(10) {
  [0]=>
  string(5) "lorem"
  [1]=>
  string(5) "ipsum"
  [2]=>
  string(3) "bla"
  [3]=>
  string(3) "bla"
  [4]=>
  string(3) "bla"
  [5]=>
  string(4) "cat1"
  [6]=>
  string(3) "bla"
  [7]=>
  string(3) "bla"
  [8]=>
  string(3) "bla"
  [9]=>
  string(13) "kids and baby"
}

