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 #172377 is a reply to message #172372] Mon, 14 February 2011 17:57 Go to previous messageGo to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma:
Senior Member
On 2/14/2011 10:34 AM, Denis McMahon wrote:
> 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

Not from the stated problem, it isn't. And again, your regex replaces
multiple \r\n or \n strings with a single one - eliminating any double
spacing the original op may want.

I really don't think a regex is appropriate for something this simple.
But if he does have \r also, it's a simple matter to:

1) replace all "\r\n" with "\n"
2) replace all "\r" with "\n"
3) replace all "\n" with "\r\n"

It maintains double spacing if required, and is faster than a regex.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
[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: Thu Nov 28 10:56:37 GMT 2024

Total time taken to generate the page: 0.04537 seconds