Re: Simple String Replace Question

From: Date: Sun, 25 Feb 2001 23:00:29 +0000
Subject: Re: Simple String Replace Question
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-41612@lists.php.net to get a copy of this message
On 25 Feb 2001 14:37:02 -0800, Jeff Oien <jeff@webdesigns1.com> wrote: >> 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 = preg_replace("/(?<!a)a(?!a)/", "-", $str); > >Man, that went right over my head. Is there a description of >how this works anywhere? Thanks for help in any case. The Regular Expression part of the online PHP manual has a lot of information (http://www.php.net/manual/en/ref.pcre.php) but it does tend to assume you already know what you need to do. Basically, the regular expression in there matches any "a" where the character before isn't an "a" (?<!a) and the character after isn't an a (?!a). Any matched characters are replaced with hyphens. For your needs, '/(?<!\n)\n(?!\n)/m' should work (the /m turns on multi-line support, which is necessary since we're working with line breaking characters). Note that you'll need to escape those backslashes in PHP if you use doublequotes.

« previous php.general (#41612) next »