473,326 Members | 2,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

please help with preg_replace how to get rid of extra new lines? I've tried so many ways

Hi all,

how come this doesn't work?????

# convert to unix new lines
$text = preg_replace("/\r\n/", "\n", $text);
# remove extra new lines
$text = preg_replace("/\n+/", "\n", $text);

is there better ways to remove extra new lines???

regards,

Sid
Jul 17 '05 #1
4 10134
On 23 Apr 2004 14:57:50 -0700, on*******@yahoo.com (Sidharta) wrote:
how come this doesn't work?????

# convert to unix new lines
$text = preg_replace("/\r\n/", "\n", $text);
# remove extra new lines
$text = preg_replace("/\n+/", "\n", $text);


What makes you think it doesn't work?

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #2
"Sidharta" <on*******@yahoo.com> wrote in message
news:23*************************@posting.google.co m...
Hi all,

how come this doesn't work?????

# convert to unix new lines
$text = preg_replace("/\r\n/", "\n", $text);
# remove extra new lines
$text = preg_replace("/\n+/", "\n", $text);


I usually do preg_replace('/[\r\n]+/', "\n", $text), mainly because I can't
remember which one comes first, \n or \r.

Jul 17 '05 #3
Because I have tried it. Infact, many other ways too. The text came
from a html file, loaded into a variable. Then I tried to remove extra
new lines using those preg calls. But everytime I looked at the
result, those extra new lines still there.

load the string from file using this function:

# read the specified file and return the content
function getTemplateString($file_path) {

# get contents of a file into a string
$handle = @fopen($file_path, 'r');
if (!$handle) {
# return empty if error
return '';
}
$contents = fread($handle, filesize($file_path));
fclose ($handle);

# return the content of the file
return $contents;
}
Andy Hassall <an**@andyh.co.uk> wrote in message news:<17********************************@4ax.com>. ..
On 23 Apr 2004 14:57:50 -0700, on*******@yahoo.com (Sidharta) wrote:
how come this doesn't work?????

# convert to unix new lines
$text = preg_replace("/\r\n/", "\n", $text);
# remove extra new lines
$text = preg_replace("/\n+/", "\n", $text);


What makes you think it doesn't work?

Jul 17 '05 #4
On 24 Apr 2004 03:09:20 -0700, on*******@yahoo.com (Sidharta) wrote:
Andy Hassall <an**@andyh.co.uk> wrote in message news:<17********************************@4ax.com>. ..
On 23 Apr 2004 14:57:50 -0700, on*******@yahoo.com (Sidharta) wrote:
>how come this doesn't work?????
>
># convert to unix new lines
>$text = preg_replace("/\r\n/", "\n", $text);
># remove extra new lines
>$text = preg_replace("/\n+/", "\n", $text);


What makes you think it doesn't work?


Because I have tried it. Infact, many other ways too. The text came
from a html file, loaded into a variable. Then I tried to remove extra
new lines using those preg calls. But everytime I looked at the
result, those extra new lines still there.


But I tried it too before posting, it looks like it works to me. I'd put the
regexp itself in single quotes, but it works in doubles with literal newline
characters in the expression anyway.

<pre>
<?php
function hexdump($data)
{
for ($i=0; $i<strlen($data); $i++)
{
printf("%02x ", ord($data{$i}));

}
print "\n";
}

$text = "a\r\n\r\n\r\nb\r\n";
hexdump($text);

# convert to unix new lines
$text = preg_replace("/\r\n/", "\n", $text);
# remove extra new lines
$text = preg_replace("/\n+/", "\n", $text);

hexdump($text);
?>
</pre>

Output:

61 0d 0a 0d 0a 0d 0a 62 0d 0a
61 0a 62 0a

You mention HTML - are you sure you want to get rid of \n newlines; perhaps
you need to remove <br> linebreaks?

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Murat Tasan | last post by:
i am implementing a custom version of the java.util.Map interface. my custom version does some encryption stuff when making modifications to the map via one of the 4 modification methods (put,...
2
by: Oleg Ogurok | last post by:
Hi all, On my web page, I have a <OBJECT DATA="mydatafile.ica" ...> which loads Citrix ICA web client. The problem is that I want to generate the mydatafile.ica on the fly (e.g. based on...
6
by: Eric Johnston | last post by:
I want the visitor to enter three numbers on the page and then click a button "generate image" which will I hope cause a generated gif image to be displayed alongside on the page. This involved...
23
by: Brian | last post by:
I am very new to C# programming and have run into a problem. I apologize for the repost of this article. For some reason, my news reader attached it to an existing thread. First off, I have...
3
by: nsh | last post by:
mailing.database.mysql, comp.lang.php subject: does "LOAD DATA" EVER work?!? I've tried EVERYTHING! version info: my isp is running my web page on a linux box with php ver. 4.4.1 according to...
3
by: kaushalgajjar | last post by:
Hi, I am into a trouble since 1 week and unable to find the solution from any community or site. I have created a small program that i want to print along with horizontal lines (like a notebook...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.