while(1) { if( index( substr( $s, $start_pos, $end_pos-$start_pos ), $start_tag ) != -1 ) { $end_pos = index( $s, $end_tag, ++$end_pos ); } else { last; # $end_pos is good } }