Forum Moderators: coopster

Message Too Old, No Replies

erg replace not working

It sort of works, but not with line breaks

         

illtron

3:09 pm on Jul 14, 2006 (gmt 0)

10+ Year Member



Am I doing something wrong here? I want to replace line breaks with </LB><LB>, but it doesn't seem to do anything. Here's the code that's giving me trouble.

$body = $_REQUEST['body'];

$pattern = '\n';
$replace = '</LB><LB>';

$body = ereg_replace($pattern , $replace , $body);

Actually, I'd like to replace any number of line breaks (it could be one, it could be 50) with "\n</LB><LB>"

This is probably pretty simple, but I'm new at this, so any pointers would be helpful.

[edited by: coopster at 3:28 pm (utc) on July 14, 2006]
[edit reason]
[1][edit reason] removed link [/edit]
[/edit][/1]

coopster

3:29 pm on Jul 14, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



First glance I noticed you do not have double quotes [php.net] surrounding your newline.
$pattern = '\n';

In order for that to be considered a newline it MUST be in double quotes.
$pattern = "\n";

illtron

4:00 pm on Jul 14, 2006 (gmt 0)

10+ Year Member



Yep, that was it. I knew it would be something stupid.

How about this? Say I have 20 line breaks between text. I don't want to replace that with

\n</LB></LB>
\n</LB></LB>
\n</LB></LB>
\n</LB></LB> ... etc.

Even if there's 20, I just want to replace it with one \n</LB></LB> What should I be looking for? I thought that it should be "\n+", but that would give me 20, and I just want one. Ideas?

coopster

4:03 pm on Jul 14, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Are you asking about Stripping out multiple line breaks and turning it into just one [webmasterworld.com]?

illtron

4:41 pm on Jul 14, 2006 (gmt 0)

10+ Year Member



That's exactly it! I don't know how I missed that when I searched. Thanks a lot!

Here's what I have now...

$pattern = "/(\r\n)+/";
$replace = "\n</LB><LB>";

$body = preg_replace("$pattern","</LB><LB>",$body);

I still do have one issue, however. The output doesn't put in my new line before each </LB><LB>. Am I doing something wrong there?

illtron

5:24 pm on Jul 14, 2006 (gmt 0)

10+ Year Member



Oops, never mind that last part. That was just a stupid mistake by me. Thanks!