hee, beginners mistake here.
I've recently been reading and learning about regular expressions (aren't they fun? :-P) and im starting to get the hang of them...
the problem with line 9 is the ? character, its a special reserved character in regular expressions.
therefore all special characters wanted as normal matches must be escaped.
I found the following document very handy on understanding the different methods with regular expressions: http://www.devguru.com/.../regexp_special_characters.html
Fixed Code:
$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{$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>";
Gives:
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
enjoy my friend.