i would like to split a string on a tag into different parts.
$string = 'Text <img src="hello.png" /> other text.';
The next function doesnt work yet on the right way.
$array = preg_split('/<img .*>/i', $string);
The output should be
array(
0 => 'Text ',
1 => '<img src="hello.png" />',
3 => ' other text.'
)
What kind of pattern should i use to get it done?
EDIT What if there are multiple tags?
$string = 'Text <img src="hello.png" > hello <img src="bye.png" /> other text.';
$array = preg_split('/(<img .*>)/i', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
And the output should be:
array (
0 => 'Text ',
1 => '<img src="hello.png" />',
3 => 'hello ',
4 => '<img src="bye.png" />',
5 => ' other text.'
)
You are in the right path. You have to set the flag PREG_SPLIT_DELIM_CAPTURE in this way:
With multiple tag edited properly the regex:
This will output: