Re: ereg_replace();

From: Date: Wed, 13 Nov 2002 07:28:30 +0000
Subject: Re: ereg_replace();
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-124175@lists.php.net to get a copy of this message
At 01:59 13.11.2002, Gustaf Sjoberg said: --------------------[snip]-------------------- >hi, >i tried to implement the code, but it does not work if the string: > >a) doesnt contain any <pre>..</pre> >b) doesnt contain any <pre> >c) doesnt contain any </pre> >d) contains multiple <pre>..</pre>'s > >so i altered it a little and this is what i came up with: Here's my original function, now tested and bugs removed, together with some comments: function remove_br_in_pre($string) { $re1 = '/(.*?)<pre>(.*?)<\/pre>(.*)/is'; $re2 = '/<br>\n?/is'; // setting a variable to "null" defines it, so we don't get // a warning for an undefined variable at the first concatenation. // unset($result) doesn't define it, and is unnecessary // as it is not "set" at this moment. $result = null; // this generates a loop that will remove multiple pre's while ($string) { // my original assignment ($arMatch = preg_match(...)) was wrong. // preg_match returns 1 on match, and 0 on no match. if (preg_match($re1, $string, $arMatch)) { $result .= $arMatch[1]; // sorry, forgot to keep the <pre></pre> pairs... // you need to add a line break instead of the <br> // to keep your code formatted $result .= '<pre>'.preg_replace($re2, "\n", $arMatch[2]).'</pre>'; $string = $arMatch[3]; } else break; } // if there are no <pre></pre> pairs, $string will be unmodified and the // loop will immediately break out at the first attempt. This appends // either the whole $string, or the remaining $string, to the result. $result .= $string; return $result; } >now, i've tried it in a few different scenarios and it seems to be working, >although the function might be redundant and far from pretty - it gets the >job done. however, i have a question; what does the "is" in //is denote? ;-) >(doesnt it feel great to have code sniplets you have no idea what they do in >your scripts? ;-)) The regex modifiers: i - make that case independent, so <pre>, <PRE>, and others are matched s - treat the input as a single line, don't stop at a newline >also, do you see any direct "bugs slash features" in the current function? Not that I'd be aware of... >thanks in anticipation, You're most welcome :) -- >O Ernest E. Vogelsinger (\) ICQ #13394035 ^ http://www.vogelsinger.at/

« previous php.general (#124175) next »