If you make use of PREG_SPLIT_DELIM_CAPTURE
you need to provide a capture within the regular expression pattern used with preg_split
.
In your current pattern:
/<img/
There is mothing to capture, that is why you see it removed (Demo):
Array
(
[0] => <p>First sentence here comes. Second sentence here it is. One more sentence. </p>
[1] => alt="amj" src="https://domain.com/images7.jpg" />
[2] => alt="Ea" src="http://domain.com/images3.jpg" />
[3] => alt="amj" src="https://domain.com/images7.jpg" />
[4] => alt="amj" src="https://domain.com/images7.jpg" />
)
However, if you create a capture out of it, it will be captured:
/(<img)/
Result (Demo):
Array
(
[0] => <p>First sentence here comes. Second sentence here it is. One more sentence. </p>
[1] => <img
[2] => alt="amj" src="https://domain.com/images7.jpg" />
[3] => <img
[4] => alt="Ea" src="http://domain.com/images3.jpg" />
[5] => <img
[6] => alt="amj" src="https://domain.com/images7.jpg" />
[7] => <img
[8] => alt="amj" src="https://domain.com/images7.jpg" />
)
As you can see, preg_split
does it's documented job and will add another split per each capture of the first capturing supgroup (it will only take the first). You then might need to extend it across the full tag, which has been outline in different other html-like-string-regex questions, for example (limited as usual with regular expressions, so blame that you use preg_* functions instead of a HTML parser if you run into issues, not the pattern itself:
/(<img [^>]*>)/
Result (Demo):
Array
(
[0] => <p>First sentence here comes. Second sentence here it is. One more sentence. </p>
[1] => <img alt="amj" src="https://domain.com/images7.jpg" />
[2] =>
[3] => <img alt="Ea" src="http://domain.com/images3.jpg" />
[4] =>
[5] => <img alt="amj" src="https://domain.com/images7.jpg" />
[6] =>
[7] => <img alt="amj" src="https://domain.com/images7.jpg" />
[8] =>
)
You would make your code more stable by using a standard HTML parser.