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
Switch to threaded view of this topic Create a new topic Submit Reply
Re: How do I do this search and replace? [message #172340 is a reply to message #172334] Sun, 13 February 2011 13:53 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/13/2011 8:32 AM, Bill B wrote:
> On 2/12/2011 9:53 PM, Jerry Stuckle wrote:
>> On 2/12/2011 9:49 PM, Evan Platt wrote:
>>> On Sat, 12 Feb 2011 21:44:27 -0500, Jerry Stuckle
>>> <jstucklex(at)attglobal(dot)net> wrote:
>>>
>>>> > If you really were the computer expert, it would take you all of 10
>>>> > minutes to find out it's my real name. I've got nothing to hide.
>>>> >
>>>>
>>>> You don't get it, stoopid. You aren't even worth the 10 minutes you
>>>> claim it would take for me to find out.
>>>
>>> And how much time have you spent claiming it's not my real name vs how
>>> much time would you have spent to find out it is my real name?
>>
>> It really doesn't matter. I don't give a damn if it's your real name or
>> not - you're not worth the time or effort to find out. Weinies like you
>> never are.
>>
>>
>>>
>>>> You're the only one here less respected than Richard, troll. Everyone
>>>> else here sees you for exactly what you are.
>>>
>>> I must have missed the vote you took.
>>
>> No, you're just too stoopid to realize it.
>>
>> I never thought anyone here could be worse than TNP, but once again I've
>> been proven wrong.
>
> You could settle your mutual accusations of [insert your insult here] by
> being the first one to show some self-control. The other one would then
> be hung by his own petard.
>
> Bill B
>

Bill, I call a troll a troll, and an arsehole an arsehole. Evan is both.

And I know TNP has to come up with his usual shitface comment. But then
he's just another troll.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: How do I do this search and replace? [message #172341 is a reply to message #172331] Sun, 13 February 2011 13:56 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/13/2011 1:11 AM, Evan Platt wrote:
> On Sat, 12 Feb 2011 22:35:43 -0700, richard<member(at)newsguy(dot)com>
> wrote:
>
>>> Really?? And you criticize other peoples web sites?
>>
>> At least I don't get a "mailto" link when I click on "gallery".
>
> Neither do I. Takes me to my photo gallery.
>
> I get nothing when I click on your site, since you're so fucking
> st00pid you have no links.
>
>> You're the stalker.
>
> Oh really? LOL. Pot. Kettle.
>
>> As your ham radio license has your home address on it,
>> that is public information. Just like the phone book.
>
> Not in a few days it won't. They had hidden it, I just renewed, so it
> appeared again.
>
> You're the one who's such a chicken shit pussy that he has no problem
> posting other people's address yet is too afraid to say where he
> lives.
>
> Oh wait, you live in an RV.
>
> Pathetic.
>
>> Wow really? I think she'd like to know she has 12 cats.
>
> http://www.facebook.com/profile.php?id=100000809061317
>
> Sorry, 11:
> "I have 4 little girls, and 7 boys (okay all of them purrrrrrrrrr) and
> yes I am married (he is male child #8). "
>
>> So you tend to think that any one with 12 cats is therefore crazy?
>
> Anyone with that many cats has issues.
>
>> Brilliant deduction Dr. Watson, we could use you in Wash DC.
>
> I thought I was Ben Matlock?
>
>>> Rarely do you post anything correct.
>>
>> More than you.
>
> ROTFLOL.. that's a good one. Like how the victim is the one on trial?
>
> Idiot.

No, Evan, you have proven yourself to be a stalker. Richard may not be
the best programmer in the world. But he doesn't stalk others.

I strongly suggest you get psychiatric help for your obsession. You
seriously need it.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: How do I do this search and replace? [message #172342 is a reply to message #172341] Sun, 13 February 2011 15:09 Go to previous messageGo to next message
Evan Platt is currently offline  Evan Platt
Messages: 124
Registered: November 2010
Karma: 0
Senior Member
On Sun, 13 Feb 2011 08:56:00 -0500, Jerry Stuckle
<jstucklex(at)attglobal(dot)net> wrote:

> No, Evan, you have proven yourself to be a stalker. Richard may not be
> the best programmer in the world. But he doesn't stalk others.

Uhh yes, he does.He has posted my home and work address to the
internet on numerous occasions, as well as numerous times a threat of
"I'm going to come to your work and kick your ass".

If you can't see that as stalking, then I'm going to have to side with
TNP - you're a troll too, on the same level as RtS.
--
To reply via e-mail, remove The Obvious and .invalid from my e-mail address.
Re: How do I do this search and replace? [message #172343 is a reply to message #172342] Sun, 13 February 2011 15:19 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/13/2011 10:09 AM, Evan Platt wrote:
> On Sun, 13 Feb 2011 08:56:00 -0500, Jerry Stuckle
> <jstucklex(at)attglobal(dot)net> wrote:
>
>> No, Evan, you have proven yourself to be a stalker. Richard may not be
>> the best programmer in the world. But he doesn't stalk others.
>
> Uhh yes, he does.He has posted my home and work address to the
> internet on numerous occasions, as well as numerous times a threat of
> "I'm going to come to your work and kick your ass".
>
> If you can't see that as stalking, then I'm going to have to side with
> TNP - you're a troll too, on the same level as RtS.

Sorry, that's not stalking. That's self defense. You're the one doing
the stalking. But then trolls and stalkers always think it's someone else.

And while I don't agree with what he did, after the stalking I've seen
YOU do, I can't say as I blame him. But it's your own fault; if you
weren't stalking him he wouldn't have published that information.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: How do I do this search and replace? [message #172344 is a reply to message #172335] Mon, 14 February 2011 01:58 Go to previous messageGo to next message
spambait is currently offline  spambait
Messages: 35
Registered: September 2010
Karma: 0
Member
In article <ij8mk6$vl6$1(at)news(dot)albasani(dot)net>, The Natural Philosopher <tnp(at)invalid(dot)invalid> wrote:
> Doug Miller wrote:
>> In article <vscel6le4f6810hekfooookik3ircj3ue3(at)4ax(dot)com>,
> evan(at)theobvious(dot)espphotography(dot)com(dot)invalid wrote:
>>> On Sat, 12 Feb 2011 20:23:54 -0500, Jerry Stuckle
>>> <jstucklex(at)attglobal(dot)net> wrote:
>>>
>>>> Sorry, Evan, you've just proven you are ignorant and a troll. You
>>>> aren't even intelligent enough to understand when he's right.
>>> What part of "Well, it didn't work for me, so if it works for someone
>>> else, more power to them." did you not understand?
>>
>> If it didn't work for you, clearly you implemented it wrong. Or, as seems
> more
>> likely, you didn't actually try it at all.
>>
>> Here's a formal proof that Richard's method is correct:
>>
>> Let A be the string <a>\r\n<b>, and B the string <c>\n<d>,where <a>, <b>,
> <c>,
>> and <d> are any arbitrary strings not containing "\n". The first operation,
>> replace all "\r\n" with "\n", changes A to <a>\n<b> and leaves B untouched.
>> The second operation, replace all "\n" with "\r\n", changes A to <a>\r\n<b>,
>> and B to <c>\r\n<d>, which is the specified result: change \n to \r\n except
>> when \n is immediately preceded with \r.
>>
>> If you would dispute that proof, demonstrate the flaw in it, or post a
>> counterexample. Otherwise, you owe Richard an apology.
>>
>> Time to man up, Evan.
>>
> Indeed. simple problems like this, it's easy to construct state machines
> for, and examine every possible combination, and produce totally
> reliable bug free code. Sadly such examples are few and far between.
>
>
> As someone else pointed out tho, what about a single '\r'?

That's outside the original specifications of the problem. Whether the
specifications *should* have included that condition is a separate issue. The
fact is, they didn't, and the solution Richard posted is a correct solution to
to problem as originally posed.
Re: How do I do this search and replace? [message #172345 is a reply to message #172344] Mon, 14 February 2011 02:25 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 14/02/11 01:58, Doug Miller wrote:

> That's outside the original specifications of the problem. Whether the
> specifications *should* have included that condition is a separate issue. The
> fact is, they didn't, and the solution Richard posted is a correct solution to
> to problem as originally posed.

Oh indeed, I only mentioned them because I felt that the OP ought to
consider the possibility that a system that can generate "\n" and "\r\n"
as line delimiters might also generate "\r", and possibly various other
combinations of "\r" and "\n".

replace any combination of one or more carriage returns and line feeds
with a single cr/lf pair:

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

Rgds

Denis McMahon
Re: How do I do this search and replace? [message #172346 is a reply to message #172307] Mon, 14 February 2011 09:01 Go to previous messageGo to next message
Curtis Dyer is currently offline  Curtis Dyer
Messages: 34
Registered: January 2011
Karma: 0
Member
The Natural Philosopher <tnp(at)invalid(dot)invalid> wrote:

> BootNic wrote:
>> On Sat, 12 Feb 2011 13:15:44 -0800 (PST)
>> "laredotornado(at)zipmail(dot)com" <laredotornado(at)zipmail(dot)com> wrote:
>>
>>> I'm using PHP 5. I want to search for all instances of the
>>> "\n" character in my $contents variable and replace it with
>>> "\r\n". However, I do not want to replace the "\n" if the
>>> character immediately before is already a "\r". How would I
>>> construct such a search and replace to do this?
>>
>> [url][/color]
[color=teal]>> http://www.regular-expressions.info/lookaround.html#lookbehind[/color]
[color=teal]>> [/url] [/color]
>>
>> using negative lookbehind
>>
>> $contents = preg_replace("/(?<!\r)\n/", "\r\n", $contents);

And if you want to account for CRs as EOLs, you might try:

// Don't replace when CRs precede LFs nor when LFs follow CRs
$contents = preg_replace(
'/ (?<!\r) \n | \r (?!\n) /x',
"\r\n",
$contents
);

> yeah I can intuitively see exactly what THAT does.

This really isn't all that difficult.

Like all tools, regexes have their uses where appropriate. To
outright dismiss them is unwise, in my opinion.

With regard to the OP's case, I don't claim regexes are superior
to all alternatives, but they are certainly viable and get the job
done.

<snip>

--
Curtis Dyer
<?$x='<?$x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>
Re: How do I do this search and replace? [message #172347 is a reply to message #172304] Mon, 14 February 2011 09:14 Go to previous messageGo to next message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
On 2/12/2011 11:51 PM, BootNic wrote:
> On Sat, 12 Feb 2011 13:15:44 -0800 (PST)
> "laredotornado(at)zipmail(dot)com"<laredotornado(at)zipmail(dot)com> wrote:
>
>> I'm using PHP 5. I want to search for all instances of the "\n"
>> character in my $contents variable and replace it with "\r\n".
>> However, I do not want to replace the "\n" if the character
>> immediately before is already a "\r". How would I construct such
>> a search and replace to do this?
>
> http://www.regular-expressions.info/lookaround.html#lookbehind
>
> using negative lookbehind
>
> $contents = preg_replace("/(?<!\r)\n/", "\r\n", $contents);
>
>

One addition: Using the double replacement strategy as described by
Richard is easier to understand (for non-regexp people), but needs 2
sweeps over the content.

In case performance matters (more specifically: if the $contents is a
huge string, or/and you have to do this replacement many times
sequentially), if might be better to use the above approach from
BootNic, because I expect it will be much faster.

If performance isn't an issue, pick whatever suits you. :-)

Regards,
Erwin Moller

--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
Re: How do I do this search and replace? [message #172349 is a reply to message #172345] Mon, 14 February 2011 09:51 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Denis McMahon wrote:
> On 14/02/11 01:58, Doug Miller wrote:
>
>> That's outside the original specifications of the problem. Whether the
>> specifications *should* have included that condition is a separate issue. The
>> fact is, they didn't, and the solution Richard posted is a correct solution to
>> to problem as originally posed.
>
> Oh indeed, I only mentioned them because I felt that the OP ought to
> consider the possibility that a system that can generate "\n" and "\r\n"
> as line delimiters might also generate "\r", and possibly various other
> combinations of "\r" and "\n".
>
I have had this using various means of combining files from different
origins.


> replace any combination of one or more carriage returns and line feeds
> with a single cr/lf pair:
>
> $contents = preg_replace("/[\r\n]+/","\r\n",$contents);
>
> Rgds
>
> Denis McMahon
Re: How do I do this search and replace? [message #172350 is a reply to message #172346] Mon, 14 February 2011 09:54 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Curtis Dyer wrote:
> The Natural Philosopher <tnp(at)invalid(dot)invalid> wrote:
>
>> BootNic wrote:
>>> On Sat, 12 Feb 2011 13:15:44 -0800 (PST)
>>> "laredotornado(at)zipmail(dot)com" <laredotornado(at)zipmail(dot)com> wrote:
>>>
>>>> I'm using PHP 5. I want to search for all instances of the
>>>> "\n" character in my $contents variable and replace it with
>>>> "\r\n". However, I do not want to replace the "\n" if the
>>>> character immediately before is already a "\r". How would I
>>>> construct such a search and replace to do this?
>>> [url][/color]
[color=royalblue]>>> http://www.regular-expressions.info/lookaround.html#lookbehind[/color]
[color=royalblue]>>> [/url] [/color]
>>>
>>> using negative lookbehind
>>>
>>> $contents = preg_replace("/(?<!\r)\n/", "\r\n", $contents);
>
> And if you want to account for CRs as EOLs, you might try:
>
> // Don't replace when CRs precede LFs nor when LFs follow CRs
> $contents = preg_replace(
> '/ (?<!\r) \n | \r (?!\n) /x',
> "\r\n",
> $contents
> );
>
>> yeah I can intuitively see exactly what THAT does.
>
> This really isn't all that difficult.
>
> Like all tools, regexes have their uses where appropriate. To
> outright dismiss them is unwise, in my opinion.
>

I think they are fine one time tools, where you are e.g. hacking a file
into a format and do it but once.

I think they are ghastly for production code, as they are in essence a
programming language in their own right, and one that is so opaque to
any sort of formal structuring, that I cant say they represent good
practice.



> With regard to the OP's case, I don't claim regexes are superior
> to all alternatives, but they are certainly viable and get the job
> done.
>


> <snip>
>
Re: How do I do this search and replace? [message #172351 is a reply to message #172347] Mon, 14 February 2011 09:55 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Erwin Moller wrote:
> On 2/12/2011 11:51 PM, BootNic wrote:
>> On Sat, 12 Feb 2011 13:15:44 -0800 (PST)
>> "laredotornado(at)zipmail(dot)com"<laredotornado(at)zipmail(dot)com> wrote:
>>
>>> I'm using PHP 5. I want to search for all instances of the "\n"
>>> character in my $contents variable and replace it with "\r\n".
>>> However, I do not want to replace the "\n" if the character
>>> immediately before is already a "\r". How would I construct such
>>> a search and replace to do this?
>>
>> http://www.regular-expressions.info/lookaround.html#lookbehind
>>

>>
>> using negative lookbehind
>>
>> $contents = preg_replace("/(?<!\r)\n/", "\r\n", $contents);
>>
>>
>
> One addition: Using the double replacement strategy as described by
> Richard is easier to understand (for non-regexp people), but needs 2
> sweeps over the content.
>
> In case performance matters (more specifically: if the $contents is a
> huge string, or/and you have to do this replacement many times
> sequentially), if might be better to use the above approach from
> BootNic, because I expect it will be much faster.
>

If performance is an issue, code a one pass loop. Preferably in
assembler :-)

regexp processing is not fast.


> If performance isn't an issue, pick whatever suits you. :-)
>
> Regards,
> Erwin Moller
>
Re: How do I do this search and replace? [message #172354 is a reply to message #172351] Mon, 14 February 2011 10:04 Go to previous messageGo to next message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
On 2/14/2011 10:55 AM, The Natural Philosopher wrote:
> Erwin Moller wrote:
>> On 2/12/2011 11:51 PM, BootNic wrote:
>>> On Sat, 12 Feb 2011 13:15:44 -0800 (PST)
>>> "laredotornado(at)zipmail(dot)com"<laredotornado(at)zipmail(dot)com> wrote:
>>>
>>>> I'm using PHP 5. I want to search for all instances of the "\n"
>>>> character in my $contents variable and replace it with "\r\n".
>>>> However, I do not want to replace the "\n" if the character
>>>> immediately before is already a "\r". How would I construct such
>>>> a search and replace to do this?
>>>
>>> http://www.regular-expressions.info/lookaround.html#lookbehind
>>>

>>>
>>> using negative lookbehind
>>>
>>> $contents = preg_replace("/(?<!\r)\n/", "\r\n", $contents);
>>>
>>>
>>
>> One addition: Using the double replacement strategy as described by
>> Richard is easier to understand (for non-regexp people), but needs 2
>> sweeps over the content.
>>
>> In case performance matters (more specifically: if the $contents is a
>> huge string, or/and you have to do this replacement many times
>> sequentially), if might be better to use the above approach from
>> BootNic, because I expect it will be much faster.
>>
>
> If performance is an issue, code a one pass loop.

That is exactly what BootNic did.

> Preferably inassembler :-)

Sure. ;-)

>
> regexp processing is not fast.

Compared to what? Assembly?

Erwin Moller

>
>
>> If performance isn't an issue, pick whatever suits you. :-)
>>
>> Regards,
>> Erwin Moller
>>


--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
Re: How do I do this search and replace? [message #172356 is a reply to message #172354] Mon, 14 February 2011 10:28 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Erwin Moller wrote:
> On 2/14/2011 10:55 AM, The Natural Philosopher wrote:
>> Erwin Moller wrote:
>>> On 2/12/2011 11:51 PM, BootNic wrote:
>>>> On Sat, 12 Feb 2011 13:15:44 -0800 (PST)
>>>> "laredotornado(at)zipmail(dot)com"<laredotornado(at)zipmail(dot)com> wrote:
>>>>
>>>> > I'm using PHP 5. I want to search for all instances of the "\n"
>>>> > character in my $contents variable and replace it with "\r\n".
>>>> > However, I do not want to replace the "\n" if the character
>>>> > immediately before is already a "\r". How would I construct such
>>>> > a search and replace to do this?
>>>>
>>>> http://www.regular-expressions.info/lookaround.html#lookbehind
>>>>

>>>>
>>>> using negative lookbehind
>>>>
>>>> $contents = preg_replace("/(?<!\r)\n/", "\r\n", $contents);
>>>>
>>>>
>>>
>>> One addition: Using the double replacement strategy as described by
>>> Richard is easier to understand (for non-regexp people), but needs 2
>>> sweeps over the content.
>>>
>>> In case performance matters (more specifically: if the $contents is a
>>> huge string, or/and you have to do this replacement many times
>>> sequentially), if might be better to use the above approach from
>>> BootNic, because I expect it will be much faster.
>>>
>>
>> If performance is an issue, code a one pass loop.
>
> That is exactly what BootNic did.
>
>> Preferably inassembler :-)
>
> Sure. ;-)
>
>>
>> regexp processing is not fast.
>
> Compared to what? Assembly?
>

almost anything.

what you need to understand is that general purpose routines like printf
or a regexp, have to do many many tests IN CASE what gets passed to them
contains one of many possibilities.

They are hugely wasteful compared with a routine dedicated to doing but
one or two things.



> Erwin Moller
>
>>
>>
>>> If performance isn't an issue, pick whatever suits you. :-)
>>>
>>> Regards,
>>> Erwin Moller
>>>
>
>
Re: How do I do this search and replace? [message #172359 is a reply to message #172347] Mon, 14 February 2011 10:40 Go to previous messageGo to next message
Curtis Dyer is currently offline  Curtis Dyer
Messages: 34
Registered: January 2011
Karma: 0
Member
Erwin Moller
<Since_humans_read_this_I_am_spammed_too_much(at)spamyourself(dot)com>
wrote:

> On 2/12/2011 11:51 PM, BootNic wrote:
>> On Sat, 12 Feb 2011 13:15:44 -0800 (PST)
>> "laredotornado(at)zipmail(dot)com"<laredotornado(at)zipmail(dot)com> wrote:
>>
>>> I'm using PHP 5. I want to search for all instances of the
>>> "\n" character in my $contents variable and replace it with
>>> "\r\n". However, I do not want to replace the "\n" if the
>>> character immediately before is already a "\r". How would I
>>> construct such a search and replace to do this?
>>
>> [url][/color]
[color=teal]>> http://www.regular-expressions.info/lookaround.html#lookbehind[/color]
[color=teal]>> [/url] [/color]
>>
>> using negative lookbehind
>>
>> $contents = preg_replace("/(?<!\r)\n/", "\r\n", $contents);
>>
>
> One addition: Using the double replacement strategy as described
> by Richard is easier to understand (for non-regexp people), but
> needs 2 sweeps over the content.
>
> In case performance matters (more specifically: if the $contents
> is a huge string, or/and you have to do this replacement many
> times sequentially), if might be better to use the above
> approach from BootNic, because I expect it will be much faster.

That was my hypothesis as well. I did some rudimentary
benchmarking and found that using the two-pass approach was
actually consistently faster (even when I altered both versions to
account for single CRs):

/* based on richard's proposed solution */
function twopass($str)
{
$str = str_replace("\r\n", "\n", $str);
return str_replace("\n", "\r\n", $str);
}

The data:

$data = "Foo bar baz,\n"
. "Quux baz bar foo!\r\n"
. "Line ending with a carriage return.\r"
. "Foo bar baz.\n";
$data = str_repeat($data . str_repeat("\n", 500), 10000);


Of course, YMMV. I didn't bother to do very thorough testing.

> If performance isn't an issue, pick whatever suits you. :-)

Exactly.

--
Curtis Dyer
<?$x='<?$x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>
Re: How do I do this search and replace? [message #172368 is a reply to message #172345] Mon, 14 February 2011 12:50 Go to previous messageGo to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
On 2/13/2011 9:25 PM, Denis McMahon wrote:
> On 14/02/11 01:58, Doug Miller wrote:
>
>> That's outside the original specifications of the problem. Whether the
>> specifications *should* have included that condition is a separate issue. The
>> fact is, they didn't, and the solution Richard posted is a correct solution to
>> to problem as originally posed.
>
> Oh indeed, I only mentioned them because I felt that the OP ought to
> consider the possibility that a system that can generate "\n" and "\r\n"
> as line delimiters might also generate "\r", and possibly various other
> combinations of "\r" and "\n".
>
> replace any combination of one or more carriage returns and line feeds
> with a single cr/lf pair:
>
> $contents = preg_replace("/[\r\n]+/","\r\n",$contents);
>
> Rgds
>
> Denis McMahon

Actually, I don't think so. The way I read the OP's original is that he
wanted \r\n and not \n. I, too, thought that he also should look at
single \r. However, I don't think he would have wanted \n\n to become
\r\n, but would have wanted it to become \r\n\r\n. At least that is the
way I read the original.

--
Shelly
Re: How do I do this search and replace? [message #172369 is a reply to message #172345] Mon, 14 February 2011 12:58 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/13/2011 9:25 PM, Denis McMahon wrote:
> On 14/02/11 01:58, Doug Miller wrote:
>
>> That's outside the original specifications of the problem. Whether the
>> specifications *should* have included that condition is a separate issue. The
>> fact is, they didn't, and the solution Richard posted is a correct solution to
>> to problem as originally posed.
>
> Oh indeed, I only mentioned them because I felt that the OP ought to
> consider the possibility that a system that can generate "\n" and "\r\n"
> as line delimiters might also generate "\r", and possibly various other
> combinations of "\r" and "\n".
>
> replace any combination of one or more carriage returns and line feeds
> with a single cr/lf pair:
>
> $contents = preg_replace("/[\r\n]+/","\r\n",$contents);
>
> Rgds
>
> Denis McMahon

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.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
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 next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
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
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 message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
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
==================
Pages (2): [ «    1  2]  Switch to threaded view of this topic Create a new topic Submit Reply
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 Dec 01 00:02:51 GMT 2024

Total time taken to generate the page: 0.02359 seconds