How can I get all text that's not in parenthesis using preg_match_all
? The reason I need to use preg_match_all
is because I want to get the index of each word.
Given sentence:
Hello how [t- are] you [t- today], Sir?
I can extract all the words inside the ( )
, which works. How can I also get all text outside the ( )
separately?
preg_match_all('/\[t-(.*?)\]/', $this->target, $targetWords, PREG_OFFSET_CAPTURE);
Output:
Array
(
[0] => Array
(
[0] => are
[1] => 47
),
[0] => Array
(
[0] => today
[1] => some number
)
)
Note: I already know about preg_split
:
$outsideParenthesis = preg_split('/\[.*?\]/', $this->target);
But this doesn't allow me to maintain the index.
Note 2: It may help to provide my end goal:
I want to take a string of custom markdown. For each word, I want to generate word objects that specify their type and content.
The reason is, I'd like to send an array of word objects in order to the frontend so I can loop through the array and generate HTML elements with classes, so I can apply styling as needed.
And I want to be able to specify any markdown within, e.g.,
Hello how [t- are] you [k- today], Sir?
Where t- is target, k- is key.
So the final array I'd like would look like:
[
[
type => 'normal'
content => 'Hello how '
],
[
type => 'target'
content => 'are'
],
[
type => 'normal'
content => ' you'
]
[
type => 'key'
content => 'today'
]
[
type => 'normal'
content => ', Sir?'
]
]
Here's my wordObjects function as of now:
private function setWordObjects($array, $type)
{
return array_map(function ($n) use ($type) {
return [
'type' => $type,
'content' => $n[0],
'index' => $n[1]
];
}, $array[1]);
}