second issue first:
one call to preg_replace...if you check out, you'll see
what i mean...use arrays...
and as long as you have them ordered in the right way, it'll execute them in
that order, so by the time the \s replace comes up all your 'bbbnbb' will be
fixed...problem then is that those newlines would get caught by the \s and
get switched to, if you want them to stay, then use [ ] to
represent a space instead of \s...might want [ \t] to catch tabs as well...
first issue: hard to see whats going on, but check your syntax. i just ran
$text = "this is some text with a newline in it right here \n  there it
echo $text;
echo '<br>';
echo preg_replace("/[ ]+\n+[ ]+/","\n",$text);
and it made the substitution fine.


-----Original Message-----
From: Ken [mailto:[EMAIL PROTECTED]]
Sent: Thursday, December 06, 2001 1:56 PM
To: Jack Dempsey
Subject: RE: [PHP] Reg ex help-Removing extra blank spaces before HTML

At 11:41 AM 12/6/01 -0500, Jack Dempsey wrote:
>using "\n" as your replacement text will do it....however, if you want a
>newline in the html, then you'll want <br>...

Using "\n" (either in single or double quotes) resulted in actual 'backslash
n' being all over my output, instead of newlines.  Hence, my inquiry.

>your first pattern will be "/[ ]+\n+[ ]+/" with a replacement of a single
>newline...then the second will be the previous regex.....check out
>for help with syntax and using arrays with preg_* functions....the first
>should catch all situations of a series of spaces [with at least one
>in between them] and substitute a newline.....

Thanks, sounds about right.  The second pattern (which sometimes includes
getting rid of newlines) won't clobber the results of the first pattern?
And are you suggesting two separate calls to the preg* function?  It sounds
like you are, if each of the two patterns also has a unique replacement

Thanks a lot!

- Ken

>check out mastering regular expressions as well.....its worth the money....
>-----Original Message-----
>From: Ken [mailto:[EMAIL PROTECTED]]
>At 02:43 PM 12/5/01 -0500, Jack Dempsey wrote:
> >$t = preg_replace('/\s+/',' ',$text);
>One more thing:
>How do I replace with newline instead of a space?  I read through the
>manuals but couldn't grasp how to do this.  \n didn't cut it.
>And, more advanced -
>The above replaces any groups of 2 or more blanks (newlines, spaces, tabs)
>with a single blank.
>Is there an easy way to replace them with EITHER a space or a newline,
>depending on whether any newlines were present in the input?
>I.e. bbbbbbbbbbbbbb would be replaced with b, but bbbbbnbbbbbnbbbb would be
>replaced with n.

PHP General Mailing List (
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to