Connecting Tech Pros Worldwide Help | Site Map

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

Member
 
Join Date: Jan 2008
Posts: 113
#1: Jun 19 '08
Hi,

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,

Chromis
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,741
#2: Jun 19 '08

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


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}/
Member
 
Join Date: Jan 2008
Posts: 113
#3: Jun 20 '08

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


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,

Chromis
Member
 
Join Date: Jan 2008
Posts: 113
#4: Jun 26 '08

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


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
  2.  
  3. paragraph1
  4.  
  5.  
  6. paragraph1 
  7.  
  8.  
  9.  
  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):

[PHP]
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;
}
[/PHP]

Thanks,

Chromis
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#5: Jun 26 '08

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


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)
Member
 
Join Date: Jan 2008
Posts: 113
#6: Jun 30 '08

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


Ah right thanks, I'll remember that!
Member
 
Join Date: Jan 2008
Posts: 113
#7: Jul 1 '08

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


Ah right thanks, I'll remember that!
Reply