3v4l.org

run code in 300+ PHP versions simultaneously
<?php $test_strings = [ '0<p>a</p>1<p>b</p>2<p>c</p>3', '0<p>a</p>1<p>b</p>2<p>c</p>', '<p>a</p>1<p>b</p>2<p>c</p>3', '<p>a</p>1<p>b</p>2<p>c</p>', '<p></p>1<p>b' ]; /** * Seperate a block of code by sub blocks. Example, removing all <script>...<script> tags from HTML kode * * @param string $str, text block * @param string $startDelimiter, string to match for start of block to be extracted * @param string $endDelimiter, string to match for ending the block to be extracted * @return array [all full blocks, whats left of string] */ function getDelimitedStrings($str, $startDelimiter, $endDelimiter) { $contents = array(); $startDelimiterLength = strlen($startDelimiter); $endDelimiterLength = strlen($endDelimiter); $startFrom = $contentStart = $contentEnd = $outStart = $outEnd = 0; while (false !== ($contentStart = strpos($str, $startDelimiter, $startFrom))) { $contentStart += $startDelimiterLength; $contentEnd = strpos($str, $endDelimiter, $contentStart); $outEnd = $contentStart - 1; if (false === $contentEnd) { break; } $contents['in'][] = substr($str, ($contentStart-$startDelimiterLength), ($contentEnd + ($startDelimiterLength*2) +1) - $contentStart); if( $outStart ){ $contents['out'][] = substr($str, ($outStart+$startDelimiterLength+1), $outEnd - $outStart - ($startDelimiterLength*2)); } else if( ($outEnd - $outStart - ($startDelimiterLength-1)) > 0 ){ $contents['out'][] = substr($str, $outStart, $outEnd - $outStart - ($startDelimiterLength-1)); } $startFrom = $contentEnd + $endDelimiterLength; $startFrom = $contentEnd; $outStart = $startFrom; } $total_length = strlen($str); $current_position = $outStart + $startDelimiterLength + 1; if( $current_position < $total_length ) $contents['out'][] = substr($str, $current_position); return $contents; } foreach($test_strings AS $string){ var_dump( getDelimitedStrings($string, '<p>', '</p>') ); }
Output for 7.1.25 - 7.1.30, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
array(2) { ["in"]=> array(3) { [0]=> string(8) "<p>a</p>" [1]=> string(8) "<p>b</p>" [2]=> string(8) "<p>c</p>" } ["out"]=> array(4) { [0]=> string(1) "0" [1]=> string(1) "1" [2]=> string(1) "2" [3]=> string(1) "3" } } array(2) { ["in"]=> array(3) { [0]=> string(8) "<p>a</p>" [1]=> string(8) "<p>b</p>" [2]=> string(8) "<p>c</p>" } ["out"]=> array(3) { [0]=> string(1) "0" [1]=> string(1) "1" [2]=> string(1) "2" } } array(2) { ["in"]=> array(3) { [0]=> string(8) "<p>a</p>" [1]=> string(8) "<p>b</p>" [2]=> string(8) "<p>c</p>" } ["out"]=> array(3) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" } } array(2) { ["in"]=> array(3) { [0]=> string(8) "<p>a</p>" [1]=> string(8) "<p>b</p>" [2]=> string(8) "<p>c</p>" } ["out"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "2" } } array(2) { ["in"]=> array(1) { [0]=> string(7) "<p></p>" } ["out"]=> array(1) { [0]=> string(5) "1<p>b" } }

preferences:
110.11 ms | 406 KiB | 173 Q