By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

ereg_replace: How to replace three or more \n with \n\n?

P: 113

I'm trying to remove line breaks from a user-inputted string and trying to do this using ereg_replace and a regular expression but struggling, hope someone can help!

I would like to make all triple or more consecutive occurences of \n, \n\n. So if the pattern found \n\n\n or \n\n\n\n and so on it would replace with \n\n.

My plan is then to split() the string into an array using the double linebreak and then output each array member enclosed in <p> elements. So what i would end up with is a neat list of paragraphs.

Here is the code I have so far:

Expand|Select|Wrap|Line Numbers
  1. ereg_replace("\n{3,}","\n\n",$inputText);
Having read up on regexp. the code above should find 3 or more consecutive occurences of \n and replace it with \n\n. Sadly it doesn't, could anyone point me in the right direction?

Thanks in advance,

Jun 19 '08 #1
Share this Question
Share on Google+
6 Replies

Expert 5K+
P: 5,058
Try using the preg_replace function instead.

I'm not sure exactly what the difference between the POISIX expressions (ereg) and the PCRE expressions (preg) are, but what you posted looks like it may be PCRE.

You may also have to add start and end tags to the expression, like:
Expand|Select|Wrap|Line Numbers
  1. /\n{3,0}/
Jun 19 '08 #2

P: 113
Hi Atli,

Thanks for the reply, I didn't know there were different types of regular expression, I'll give that a go and see what I get.

Thanks again,

Jun 20 '08 #3

P: 113
Hi again,

Ok with your help i've figured it out, a bit of trial and error was necessary!

The following is my input string:

Expand|Select|Wrap|Line Numbers
  1. Enter text here
  3. paragraph1
  6. paragraph1 
  10. paragraph1
After expression: preg_replace("/[\r\n]{2,}/"," break\n",$inputText);

Expand|Select|Wrap|Line Numbers
  1. Enter text here break
  2. paragraph1 break
  3. paragraph1 break
  4. paragraph1
The key was using the \r\n characters (im on windows) in a character range. I probably should have said the input was from a text box.

I'm writing my first class to format input copy and pasted from a word doc (has lots of incorrect characters and in most cases too many line breaks).

Here is my code so far (any feedback is welcome):

class FormatForWeb {

public $inputText;
public $outputText;

// Recieve input text
public function __construct($inputText) {
$this->inputText = $inputText;
// Output text as HTML paragraphs
public function paragraphFormat() {
// Replace triple or more occurences of \r\n with just two.
$inputText = preg_replace("/[\r\n]{2,}/","\n\n",$this->inputText);

/* Break into paragraphs */
$inputText = split("\n\n",$inputText);
foreach($inputText as $para) {
if($para!="") $this->outputText = $this->outputText . "<p>" . $para . "</p>\n";
return $this->outputText;


Jun 26 '08 #4

Expert 5K+
P: 5,821
Heya, Chromis.

Try using the constant PHP_EOL, which gets set based on your operating system's settings. Should you ever port your code to a Linux server, you won't have to change your code.

(By the way, PHP_EOL appears on the Zend PHP 5 Certification Exam)
Jun 26 '08 #5

P: 113
Ah right thanks, I'll remember that!
Jun 30 '08 #6

P: 113
Ah right thanks, I'll remember that!
Jul 1 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.