Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reg Exp help needed 3

Status
Not open for further replies.

Dweezel

Technical User
Feb 12, 2004
428
GB
I'm hoping that someone can help me out with a regular expression pattern that I need, to use in a preg_replace function.

I need to parse text being retrieved from a database, replacing line breaks with paragraph tags. The complicated bit is that the no matter how many line breaks there are following each other, or if there are spaces in between, only one set of paragraph tags must be produced:

Example:

Code:
example1: $string = This is some text /n/n This is some more text.

example2: $string = This is some text /r    /r    /r This is some more text.

example3: $string = This is some text /n/n/n/n/n/n This is some more text.

No matter which of the previous strings are parsed I need the output to be:

Code:
 This is some text </p><p> This is some more text.

So if my preg_replace were as follows:
Code:
 preg_replace($pattern,'</p><p>',$string)

Can anyone help me out with the $pattern?

TIA.
 
what about this?

Code:
$pattern = "/(\n|\r)+/";

it reads "one or more instances of either \n or \r". i haven't tested it, but i believe that's the general premise.



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Thanks clflava.

It almost works perfectly. I'm still getting a few double sets of closing and opening p tags. I'm guessing this is where spaces are placed between the line breaks. Any idea how to modify the pattern you posted to include possible spaces?

Thanks.
 
have you thought of this pcode:
1. split the string into array chunks by \r or\n
2. array_pop each element that does not contain anything but spaces
3. implode the array and add para marks as the glue


this may actually be quicker than a regex
 
I would add an allowance for white space to support the second example:
Code:
$pattern = "/[\n\r]+(\s+[\n\r])?/"
So at least one \n or \r, optionally followed by as much whitespace as you want, as long as it has a \r or \n after it. Basically this ensures you don't lose any trailing spaces off the line before or prefixed to the next line. If you didn't care about whitespace you could also do:
Code:
$pattern = "/\s*[\n\r]\s*?/"
Basically match any amount of whitespace provided there's at least a single \n or \r somewhere.

-T

signature.png
 
Hmm, "was" Cory? j/k. ;)

I'd be interested to see a comparison between a preg_replace and jpadies method. I'd think it would be faster to use a single preg_replace, but I'm still fairly new to PHP.

signature.png
 
Sweet!! All three suggestions worked.

I'm really going to have to take some time out and learn regular expressions properly. Maybe the next time I get fired. Either that or when I retire. :)

Thanks a lot fellas.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top