Re: ereg_replace();
| From: | Gustaf Sjoberg | Date: | Tue, 12 Nov 2002 17:44:29 +0000 |
| Subject: | Re: ereg_replace(); | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-124035@lists.php.net to get a copy of this message | ||
many thanks, and kudos for the quick reply. i will try that right away!
as a sub-question, do you mind telling me where you learned regexp? i've been searching google
all day with no luck, i've just find more or less basic regexp guides. did you learn through
practice or do you have a secret source? ;-)
On Tue, 12 Nov 2002 18:35:55 +0100
ernest@vogelsinger.at (Ernest E Vogelsinger) wrote:
>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/
>
--
Gustaf Sjoberg <gs@vacfu.org>
<(" <) <(" )> <( ")> (> ")>