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:
- Enter text here
-
-
paragraph1
-
-
-
paragraph1
-
-
-
-
paragraph1
After expression: preg_replace("/[\r\n]{2,}/"," break\n",$inputText);
- 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($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