473,508 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

113 New Member
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
Jun 19 '08 #1
6 2442
Atli
5,058 Recognized Expert Expert
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
chromis
113 New Member
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
Jun 20 '08 #3
chromis
113 New Member
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
Jun 26 '08 #4
pbmods
5,821 Recognized Expert Expert
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
chromis
113 New Member
Ah right thanks, I'll remember that!
Jun 30 '08 #6
chromis
113 New Member
Ah right thanks, I'll remember that!
Jul 1 '08 #7

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

Similar topics

2
2176
by: Chris | last post by:
Hello, I use a javascript ticker in my header which contains the news in a array like that tickercontents='Message1' tickercontents='Message2' tickercontents='Message3' now i want to write an...
4
2413
by: knocte | last post by:
Hello. I am testing this curious code and I don't understand yet why it doesn't work with the 2nd case: Case 1: $number = "2,4"; $number = ereg_replace(",",".",$number); echo $number; //...
3
1609
by: Evelyne | last post by:
Hi all, I use: $text=ereg_replace ('+(\.+)*\@' . '+(\.{1,3})', '<a href="mailto:\\0">\\0</a>',$text); to replace all e-mailadresses within a block of text to clickable links. I wonder if...
9
1835
by: Xah Lee | last post by:
© # -*- coding: utf-8 -*- © # Python © © import sys © © nn = len(sys.argv) © © if not nn==5: © print "error: %s search_text replace_text in_file out_file" % sys.argv
52
4551
by: DJ WIce | last post by:
Hi all, I did make a script/css thing to replace the contextmenu on the website with a new one: http://www.djwice.com/contextmenu.html It works nice in MSIE, but on Netscape (and probable...
0
1088
by: pieterprovoost | last post by:
Hi, I would like to use the ereg_replace() function to replace wiki-like syntax, for example: ] becomes <a href="index.php?page=somepage">somepage</a> **hello** becomes <i>hello</i>
6
536
by: Bob Bedford | last post by:
Hello there, I need help: I've a string that is used to write down a phone number that comes from a form and would like to keep it as clean as possible. For avoiding people to create twice an...
1
4162
by: tmcp | last post by:
Hello I'm a bit new to regular expressions and I'm having a problem I'm trying to strip any code which is inside a <p> tag for instance from <p style="margin: 0in 0in 0pt;" class="MsoNormal"> to...
2
1505
by: encepif | last post by:
I think this is the right command., could someone please show me how to replace instances of a quote like this " with its html version - &quot;. I am mixed up with the escaping, etc. Thank you :-) ...
0
7226
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7125
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7049
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4709
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.