![]() |
|
#1
|
|||
|
|||
regular expressionsI'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:
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
|
|||
|
|||
|
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:
|
|
#3
|
||||
|
||||
|
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:
Or... if you don't care much about displaying multiple paragraphs; use nl2br() __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#4
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
About which is faster; when you can use either; always go with str_replace()... __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#6
|
|||
|
|||
|
Quote:
Soz to be a pain, but curious as to why you should always use str_replace()? |
|
#7
|
||||
|
||||
|
Straight from the manual:
Quote:
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... __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
Recent GIDBlog
2nd Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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