FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » How do I do this search and replace?
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: How do I do this search and replace? [message #172372 is a reply to message #172369] Mon, 14 February 2011 15:34 Go to previous messageGo to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
On 14/02/11 12:58, Jerry Stuckle wrote:
> On 2/13/2011 9:25 PM, Denis McMahon wrote:

>> $contents = preg_replace("/[\r\n]+/","\r\n",$contents);

> That also will replace things like \r\n\r\n or \n\n with a single \r\n.

> Richard's answer is better - it solves the stated problem correctly.

I agree that Richard's answer is fine for the OP's stated problem.

I'm just suggesting to the OP that there might be other cases in his
input that he might want to consider:

(a) singleton \r (I don't know how his data is being created, but it
seems inherently flawed to me to assume that whatever "system" generates
data that can contain both "\r\n" and "\n" sequences will never generate
data that might contain a singleton "\r")

(b) chains of more than one "\r\n" or "\n" or even "\r" (it will be for
the OP to consider whether he wants to replace such chains with a single
"\r\n")

Given that both \n and \r are included in \s, perhaps even:

$contents = preg_replace("/\s*[\r\n]+\s*/","\r\n",$contents);

replacing 'any sequence of whitespace that includes at least one "\n" or
"\r"' with a single cr/lf pair.

could be useful to the OP?

Rgds

Denis McMahon
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Curious problem
Next Topic: Hot Sameera Hot Cleavage show - Download
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 10 13:28:47 GMT 2024

Total time taken to generate the page: 0.05128 seconds