At 17:49 12.11.2002, Gustaf Sjoberg spoke out and said:
--------------------[snip]--------------------
>Hi,
>i'm trying to replace every instance of "<br>" within the "<pre>..</pre>" 
>tags. i want all other breakrows to remain the same.
>
>i've tried numerous regular expressions, but i can't find a way to just 
>replace the breakrows.. it replaces _everything_ bewteen <pre> and </pre>.
--------------------[snip]-------------------- 

You need a two-phase operation on this:

1) isolate all <pre></pre> elements
   preg_match('/(.*?)<pre>(.*?)<\/pre>(.*)/is', $string, $armatch);

$armatch now contains:
   [0] all, ignore this
   [1] everything before <pre>
   [2] everything within <pre></pre>
   [3] the rest after </pre>

2) go on and replace all <br>'s here:
   preg_replace('/<br>\n?/is', "\n", $armatch[2]);

The whole stuff would look something like

/* UNTESTED */
function kill_br_within_pre($string)
{
    $re1 = '/(.*?)<pre>(.*?)<\/pre>(.*)/is';
    $re2 = '/<br>\n?/is';
    $result = null;

    while ($string) {
        $armatch = preg_match($re1, $string, $arMatch);
        if (is_array($arMatch)) {
            $result .= $arMatch[1];
            $result .= preg_replace($re2, "\n", $arMatch[2]);
            $string = $arMatch[3];
        }
        else break;
    }
    $result .= $string;
    return $result;
}

This assumes that all <pre> are properly closed with </pre>. As said, I
didn't test, but it should work this or a similar way.


-- 
   >O Ernest E. Vogelsinger 
   (\) ICQ #13394035 
    ^ http://www.vogelsinger.at/

Reply via email to