Re: ereg_replace();
| From: | Gustaf Sjoberg | Date: | Wed, 13 Nov 2002 00:59:11 +0000 |
| Subject: | Re: ereg_replace(); | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-124123@lists.php.net to get a copy of this message | ||
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:
<?
function remove_br_in_pre($string)
{
unset($result);
if (!preg_match('/(.*?<pre>)(.*?)(<\/pre>)(.*)/is', $string))
{
if (!preg_match('/<pre>/is', $string))
return $string;
else
$string .= "</pre>";
}
while ($string)
{
preg_match('/(.*?<pre>)(.*?)(<\/pre>)(.*)/is', $string,
$arMatch);
if (is_array($arMatch))
{
$result .= $arMatch[1];
$result .= str_replace('<br>','',$arMatch[2]);
$result .= $arMatch[3];
if (!preg_match('/<pre>/is', $arMatch[4]))
{
$result .= $arMatch[4];
return $result;
}
$string = $arMatch[4];
}
else break;
}
}
//test string
$param =
"test<br>test<br><pre><br>testing<br>testing<br></pre>test<br>test<br>test<pre>test<br>test</pre>testing<br>test";
echo remove_br_in_pre($param);
?>
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? ;-))
also, do you see any direct "bugs slash features" in the current function? i'm not
usually asking someone who spends his freetime helping others "baby sit" my code, but this
was my first day using regular expressions so i've been taking a few sure shots.. so it would
be great if you could just take a quick glance at it.
thanks in anticipation,
GS
On Tue, 12 Nov 2002 18:57:04 +0100
ernest@vogelsinger.at (Ernest E Vogelsinger) wrote:
>At 18:44 12.11.2002, Gustaf Sjoberg spoke out and said:
>--------------------[snip]--------------------
>>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? ;-)
>--------------------[snip]--------------------
>
>This stems from my old Perl days - I recommend reading the "Camel Book" :)
>
>Refer to http://www.perldoc.com/perl5.6/pod/perlre.html and eat
>this page -
>you'll be a RegEx whiz in seconds (naahh - it takes a bit longer ;->)
>
>
>--
> >O Ernest E. Vogelsinger
> (\) ICQ #13394035
> ^ http://www.vogelsinger.at/
>
--
Gustaf Sjoberg <gs@vacfu.org>
<(" <) <(" )> <( ")> (> ")>