...

View Full Version : Clean and keep line breaks



Trki
10-14-2012, 01:02 PM
Hellou ... i want to keep line spaces in comments but clean more then 2 after each.

Example:

$sentence="This is my first sentence
This will be in a new row



This will be too in a new row but not separated with 3line breaks just with


one



";
And i want to output just this:

$sentence="This is my first sentence
This will be in a new row

This will be too in a new row but not separated with 3line breaks just with

one
";

Any ideas? :P

sunfighter
10-14-2012, 05:59 PM
You do not have line breaks in the php you have shown. You have white space, php does not see white space. And to php it looks like this:
$sentence="This is my first sentence This will be in a new row This will be too in a new row but not separated with 3line breaks just with one";

Are you talking about something like this:
$sentence="This is my first sentence.'<br />'.
This will be in a new row.'<br /><br /><br />'.
This will be too in a new row but not separated with 3line breaks just with.'<br /><br /><br />'.
one.'<br /><br /><br />'
";

Inigoesdr
10-14-2012, 06:16 PM
Not sure if this is exactly what you are going for, but you can try something like this:

$sentence="This is my first sentence
This will be in a new row



This will be too in a new row but not separated with 3line breaks just with


one



";

$clean = trim(preg_replace('/[\r\n]{3,}/s', "\n\n", $sentence));

var_dump($clean);

Outputs:

string(135) "This is my first sentence
This will be in a new row

This will be too in a new row but not separated with 3line breaks just with

one"


You do not have line breaks in the php you have shown. You have white space, php does not see white space. And to php it looks like this:
$sentence="This is my first sentence This will be in a new row This will be too in a new row but not separated with 3line breaks just with one";
That's not entirely true. While it is true that whitespace in between *code* is ignored, spaces/newlines/carriage returns inside of strings are not. If you echo that string wrapped in a <pre> tag or view the source of the page in your browser you would see the extra newlines OP is referring to. Granted, they would be collapsed when the page is rendered normally in the browser(not in a <pre> tag or <textarea>), but they are still part of the string inside of the variable and in the raw output.

patryk
10-14-2012, 06:24 PM
You do not have line breaks in the php you have shown. You have white space, php does not see white space. And to php it looks like this:
$sentence="This is my first sentence This will be in a new row This will be too in a new row but not separated with 3line breaks just with one";

Are you talking about something like this:
$sentence="This is my first sentence.'<br />'.
This will be in a new row.'<br /><br /><br />'.
This will be too in a new row but not separated with 3line breaks just with.'<br /><br /><br />'.
one.'<br /><br /><br />'
";
of course it does....
if you don't believe, just run this:


<?php
$data = 'line 1
line 2
line 3
line 4
line 5';
$pieces = explode("\n", $data);
print_r($pieces);
?>


as for original question: try this:


<?php
$data = 'line 1
line 2



line 3



line 4



line 5';
echo preg_replace('#\n{2,999}#', "\n\n", $data);
?>

this code will output this:


line 1
line 2

line 3

line 4

line 5


---edit---
damn second again... lol

Trki
10-14-2012, 08:24 PM
[QUOTE=Inigoesdr;1280078]Not sure if this is exactly what you are going for, but you can try something like this:

$sentence="This is my first sentence
This will be in a new row



This will be too in a new row but not separated with 3line breaks just with


one



";


$clean = trim(preg_replace('/[\r\n]{3,}/s', "\n\n", $sentence));

var_dump($clean);

Outputs:

string(135) "This is my first sentence
This will be in a new row

This will be too in a new row but not separated with 3line breaks just with

one"

Yes this "$clean = trim(preg_replace('/[\r\n]{3,}/s', "\n\n", $sentence));" was working good. But here is one more thing... Now the script works only when i add the css 'white-space' to the td box but how to delete the top white space? :P I dont know why its there.

http://www.upnito.sk/0/fr5rr2may5dcf8qt3vu9wdb2nyfnjetz.png

Trki
10-15-2012, 11:19 AM
[QUOTE=Inigoesdr;1280078]Not sure if this is exactly what you are going for, but you can try something like this:

$sentence="This is my first sentence
This will be in a new row



This will be too in a new row but not separated with 3line breaks just with


one



";


$clean = trim(preg_replace('/[\r\n]{3,}/s', "\n\n", $sentence));

var_dump($clean);

Outputs:

string(135) "This is my first sentence
This will be in a new row

This will be too in a new row but not separated with 3line breaks just with

one"

Yes this "$clean = trim(preg_replace('/[\r\n]{3,}/s', "\n\n", $sentence));" was working good. But here is one more thing... Now the script works only when i add the css 'white-space' to the td box but how to delete the top white space? :P I dont know why its there.

http://www.upnito.sk/0/fr5rr2may5dcf8qt3vu9wdb2nyfnjetz.png

and the white space at picture means that there is a white space before text when i do (inspect elemenet in chrome and check it i see white space before text) So i need somehow modify the function to cut the whitespace before text hm...

Dormilich
10-15-2012, 01:12 PM
just because there is white space does not mean there is whitespace. you can get that as well with margins or paddings without the use of whitespace at all. wee would need to see the HTML/CSS code for that to be clear.

patryk
10-15-2012, 03:02 PM
try this:


$data = '


line 1
line 2



line 3

line 4


line 5

';
$data = preg_replace('#[\r\n]+#', "\n", $data); //just in case...

$data = preg_replace('#^\s+\n#', '', $data); //strip first line if contains only spaces, tabs etc...

$data = preg_replace('#[\t ]+#', ' ', $data); //strip multiple spaces and tabs

$data = preg_replace('#\s+$#', ' ', $data); //strip whitespaces and \n's ending string

echo preg_replace('#(\n| \n){2,}#', "\n\n", $data);

works on my server

Trki
10-15-2012, 06:28 PM
try this:


$data = '


line 1
line 2



line 3

line 4


line 5

';
$data = preg_replace('#[\r\n]+#', "\n", $data); //just in case...

$data = preg_replace('#^\s+\n#', '', $data); //strip first line if contains only spaces, tabs etc...

$data = preg_replace('#[\t ]+#', ' ', $data); //strip multiple spaces and tabs

$data = preg_replace('#\s+$#', ' ', $data); //strip whitespaces and \n's ending string

echo preg_replace('#(\n| \n){2,}#', "\n\n", $data);

works on my server

didnt help man ... still this:

http://www.upnito.sk/0/49zc2a4qdq756t3p2637tq7jb8bhsbrz.png

Dormilich
10-15-2012, 06:30 PM
maybe you’re not trimming the right string?

Trki
10-15-2012, 06:57 PM
maybe you’re not trimming the right string?

ah yes i am because i made there a lot of spaces and in trimed to max one which is gr8 it just didnt cleared the white space from forward of the string how u can see

Trki
10-17-2012, 09:22 PM
hm.. so nobody know how to fix the white spat there? :P

Inigoesdr
10-18-2012, 03:57 AM
Where are you outputting it at? trim() (http://php.net/trim) removes the whitespace from both ends of the string.

Trki
10-18-2012, 09:25 AM
Where are you outputting it at? trim() (http://php.net/trim) removes the whitespace from both ends of the string.

aaaaaah i fixed it.. there was problem not with function... but there were white spaces directly in code between tags... i didnt realize that it can be problem. Thx to all of you and sorry for my mistake in last point :P



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum