Jump to content

Recommended Posts

Hello I have this code below that converts all types of line breaks to \r\n.

 

$text = preg_replace('#[\r\n]+#', "\n", $text);

 

But if there are 5 line breaks it groups them to 1.  I want to keep the number of line breaks the same, just make them all the same format.    If I remove the + it seems to double the actual number of line breaks.  Can anyone help?

Link to comment
https://forums.phpfreaks.com/topic/187268-line-breaks/
Share on other sites

Just use str_replace as this is rather simple:

 

$text = str_replace("\r\n", "\n", $text);

 

You generally only want to use the preg functions when you need advance replacement / finding of contents inside a string. Since you do not, str_replace will due and would be preferred.

Link to comment
https://forums.phpfreaks.com/topic/187268-line-breaks/#findComment-988899
Share on other sites

Just \r\n. Please do not create multiple topics on the same subject. One will suffice, for future reference.

 

Try this for your preg code:

$text = preg_replace("#[\r\n]+#", "\n", $text);

 

As the \r\n characters are taken literally inside of single quotes, but inside of double quotes they are the actual linebreak characters.

 

EDIT:

If you want to be able to see it by reading it you can use this to verify that it works:

$text = preg_replace("#[\r\n]+#", " REPLACED ", $text);

 

That way, any \n, \r etc characters that get replaced are visible by the REPLACED word.

Link to comment
https://forums.phpfreaks.com/topic/187268-line-breaks/#findComment-988902
Share on other sites

I do not know how you are getting double line breaks. Using this code it:

 

<?php
$text = "this is just \r test \r\n text inside \n of text \n\r more text";

$text2 = preg_replace("#[\r\n]+#", "REPLACED", $text);
echo $text2 . "<br /><pre>";

$text = preg_replace("#[\r\n]+#", "\n", $text);
echo $text;
?>

 

Displays this:

this is just REPLACED test REPLACED text inside REPLACED of text REPLACED more text

this is just 
test 
text inside 
of text 
more text

 

Like it should.

Link to comment
https://forums.phpfreaks.com/topic/187268-line-breaks/#findComment-988912
Share on other sites

but what if there are two line breaks as below:

 

$text = "this is just \r test \r\n\r\n text inside \n of text \n\r more text";

 

the echo should be:

 

this is just

test

 

text inside

of text

more text

 

but instead it is:

 

this is just

test

text inside

of text

more text

 

How can I keep the multiple line breaks?

Link to comment
https://forums.phpfreaks.com/topic/187268-line-breaks/#findComment-988916
Share on other sites

<?php
$text = "this is just \r test \r\n text inside \n of text \n\r more text \r\n\r\n after the window \n\r\n\r ok";

$text = preg_replace("#[\r\n]{1,2}#", "\n", $text);
echo $text;

 

Should give:

this is just 
test 
text inside 
of text 
more text 

after the window 

ok

Link to comment
https://forums.phpfreaks.com/topic/187268-line-breaks/#findComment-988921
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.