<?php
/// starting string
$string = '%%this is line 1 level 1??this is line 1 level 2%%this is line 2 level 1%%this is line 3 level 1??this is line 3 level 2%%this is line 4 level 1??this is line 4 level 2&&this is line 4 level3';
/// what an indent looks like
$indent = " --";
/// replace any %% with one indent worth
$result = preg_replace( '/\%\%/', "\n\n{$indent}", $string );
/// replace any ?? with two indents worth
$result = preg_replace( '/\?\?/', "\n{$indent}{$indent}", $result );
/// replace any && with three indents worth
$result = preg_replace( '/\&\&/', "\n{$indent}{$indent}{$indent}", $result );
/// remove any extra newlines placed at the beginning of the result
$result = preg_replace( '/^\n+/', '', $result );
echo "<pre>";
echo "\nORIGINAL:\n";
echo $string;
echo "\n\nRESULT:\n";
echo $result;
echo "</pre>";
?>
optput looks like
ORIGINAL:
%%this is line 1 level 1??this is line 1 level 2%%this is line 2 level 1%%this is line 3 level 1??this is line 3 level 2%%this is line 4 level 1??this is line 4 level 2&&this is line 4 level3
RESULT:
--this is line 1 level 1
-- --this is line 1 level 2
--this is line 2 level 1
--this is line 3 level 1
-- --this is line 3 level 2
--this is line 4 level 1
-- --this is line 4 level 2
-- -- --this is line 4 level3
[edit]
There are some ways to optimize the preg_replace calls into one call but ill leave that as an exercise to the reader, and this should really be wrapped up in a function.. but this is more better like looking i think