GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Mar-2004, 19:22
erniegerdie erniegerdie is offline
Junior Member
 
Join Date: Feb 2004
Location: England
Posts: 70
erniegerdie will become famous soon enough

regular expressions


I'm trying to write a function which takes text from a <textarea> submitted in a form, and places lines which are seperated by a new line characters into html paragraphs.

Here's the code.
PHP Code:

<?php
function replaceparagraphs($string) {
    $pattern = "/(.*)(\r|\n)*?\r\n/";
    $replacement = "<p>$1</p>\n";
    return preg_replace($pattern, $replacement, $string);
}

echo replaceparagraphs($_GET['data']);
?>


Example input:
Test data1

Test data2

Test data3

This produces the ouput:
<p>Test data1
</p>
<p>Test data2
</p>
<p>Test data3</p>

My question is, how do I get the regular expression to remove the second new line chararater?

So the the output produced is:
<p>Test data1</p>
<p>Test data2</p>
<p>Test data3</p>
  #2  
Old 08-Mar-2004, 04:25
erniegerdie erniegerdie is offline
Junior Member
 
Join Date: Feb 2004
Location: England
Posts: 70
erniegerdie will become famous soon enough
I found out what the problem was. I had to place a match in (.*) to indicate that I did not want to retrieve the new lines or carriage returns. Done with [^\r\n]. I also had to put in a $ to catch the last string just before the end of the string. Lastly ? was making the preceding match null and void.

New code:
PHP Code:

function replaceparagraphs($string) {
    $pattern = "/(.[^\r\n]*)($|\r|\n)*($|(\r\n))/";
    $replacement = "<p>$1</p>\n";
    return preg_replace($pattern, $replacement, $string);
}

echo replaceparagraphs($_GET['data']); 


  #3  
Old 08-Mar-2004, 04:46
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
A few quick questions first... why $_GET['data'], not $_POST['data']?

What is the pattern $pattern = "/(.*)(\r|\n)*?\r\n/"; supposed to match; I looked at it for a while and I still don't get it...

Instead of preg_replace(), I would suggest str_replace() (or even strtr()) to accomplish the same thing... something like this for example:

PHP Code:

<?php
function replaceparagraphs( $string )
{
  $string   = trim( $string );  
  $find     = array( "\r\n\r\n", "\n\n", "\r\r", "\r\n", "\n", "\r", '{NL}' );
  $replace  = array( 
                '</p>{NL}<p>',
                '</p>{NL}<p>',
                '</p>{NL}<p>',
                '<br />{NL}',
                '<br />{NL}',
                '<br />{NL}',
                "\n"
              );
  return( '<p>'.str_replace($find, $replace, $string).'</p>' );
} 
?>


Or... if you don't care much about displaying multiple paragraphs; use nl2br()
  #4  
Old 08-Mar-2004, 05:15
erniegerdie erniegerdie is offline
Junior Member
 
Join Date: Feb 2004
Location: England
Posts: 70
erniegerdie will become famous soon enough
No reason why I'm using the $_GET, this is just a test script so far.

The pattern was a bit dodgy, I'm hoping that the pattern in my last post makes more sense. I wanted to use a regular expression, because I wanted some practice in using them.

By the way which way is faster, using the preg_replace or using the str_replace?

cheers
  #5  
Old 08-Mar-2004, 05:24
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Quote:
Originally Posted by erniegerdie
...The pattern was a bit dodgy, I'm hoping that the pattern in my last post makes more sense. I wanted to use a regular expression, because I wanted some practice in using them....
I understand...

About which is faster; when you can use either; always go with str_replace()...
  #6  
Old 08-Mar-2004, 05:34
erniegerdie erniegerdie is offline
Junior Member
 
Join Date: Feb 2004
Location: England
Posts: 70
erniegerdie will become famous soon enough
Quote:
Originally Posted by JdS
always go with str_replace()...

Soz to be a pain, but curious as to why you should always use str_replace()?
  #7  
Old 08-Mar-2004, 05:46
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Straight from the manual:

Quote:
If you don't need fancy replacing rules, you should always use this function (i.e. str_replace) instead of ereg_replace() or preg_replace().

Besides, I have read many user comments and articles that verify this to be the case...

Like I said, if you can use EITHER, use str_replace(); there are times when you can't use str_replace and then your second choice is obviously preg_replace; I NEVER use ereg_replace, so I don't know...
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
preg_replace question... Perculator MySQL / PHP Forum 6 16-Feb-2004 21:25
preg_replace + regular expression EasyExpat MySQL / PHP Forum 4 10-Jul-2003 04:36
Regular Expressions question. JdS MySQL / PHP Forum 3 04-Nov-2002 14:04

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:01.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.