Re: Simple String Replace Question
| From: | Simon Garner | Date: | Sun, 25 Feb 2001 22:49:19 +0000 |
| Subject: | Re: Simple String Replace Question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-41607@lists.php.net to get a copy of this message | ||
From: "Jeff Oien" <jeff@webdesigns1.com>
> > On 25 Feb 2001 10:34:27 -0800, Jeff Oien <jeff@webdesigns1.com> wrote:
> > >I would like to get rid of \n characters unless there
> > >are two or more in a row. So for example if there
> >
> > The Perl-compatible regular expressions support lookahead and look
behind:
> >
> > $str = 'abcdefabcaadd';
> > echo "$str\n";
> > $str = preg_replace("/(?<!a)a(?!a)/", "-", $str);
> > echo "$str\n";
> >
> > will display this:
> > abcdefabcaadd
> > -bcdef-bcaadd
>
> Man, that went right over my head. Is there a description of
> how this works anywhere? Thanks for help in any case.
> Jeff Oien
>
Or you could just do this:
<?php
$str = "abc\ndefg\n\nxyzpqr\njklmno";
$str = ereg_replace("([^\n])\n([^\n])", "\\1 \\2", $str);
echo $str;
?>
That should give you:
abc defg\n\nxyzpqr jklmno
Works by replacing any \n with a space, as long as that \n is not next to
another \n.
Cheers
Simon Garner