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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chromis
    New Member
    • Jan 2008
    • 113

    #1

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

    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:

    Code:
    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
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    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:
    Code:
    /\n{3,0}/

    Comment

    • chromis
      New Member
      • Jan 2008
      • 113

      #3
      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

      Comment

      • chromis
        New Member
        • Jan 2008
        • 113

        #4
        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:

        Code:
        Enter text here
        
        paragraph1
        
        
        paragraph1 
        
        
        
        paragraph1
        After expression: preg_replace("/[\r\n]{2,}/"," break\n",$input Text);

        Code:
        Enter text here break
        paragraph1 break
        paragraph1 break
        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($in putText) {
        $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",$t his->inputText);

        /* Break into paragraphs */
        $inputText = split("\n\n",$i nputText);
        foreach($inputT ext as $para) {
        if($para!="") $this->outputText = $this->outputText . "<p>" . $para . "</p>\n";
        }
        return $this->outputText;
        }
        [/PHP]

        Thanks,

        Chromis

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          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)

          Comment

          • chromis
            New Member
            • Jan 2008
            • 113

            #6
            Ah right thanks, I'll remember that!

            Comment

            • chromis
              New Member
              • Jan 2008
              • 113

              #7
              Ah right thanks, I'll remember that!
              Last edited by chromis; Jul 1 '08, 08:50 AM. Reason: Duplicate

              Comment

              Working...