Skip to content

Instantly share code, notes, and snippets.

@lonalore
Created August 16, 2019 14:08
Show Gist options
  • Save lonalore/7e00a56003215461e23f37b5b1f3737e to your computer and use it in GitHub Desktop.
Save lonalore/7e00a56003215461e23f37b5b1f3737e to your computer and use it in GitHub Desktop.
PHP split string on <img> tag
<?php
$string = 'Text <img src="hello.png" > hello <img src="bye.png" /> other text.';
$array = preg_split('/(<img[^>]+\>)/i', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
// array(5) {
// [0]=>
// string(5) "Text "
// [1]=>
// string(22) "<img src="hello.png" >"
// [2]=>
// string(7) " hello "
// [3]=>
// string(21) "<img src="bye.png" />"
// [4]=>
// string(12) " other text."
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment