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 #172359 is a reply to message #172347] Mon, 14 February 2011 10:40 Go to previous message
Curtis Dyer is currently offline  Curtis Dyer
Messages: 34
Registered: January 2011
Karma:
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);?>
[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:29:26 GMT 2024

Total time taken to generate the page: 0.03002 seconds