Carriage returns

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am formatting a plain text file into HTML and trying to find the
carriage returns with little success...

$s = file_get_contents('./test.txt')
$search =  array ('\n\n');
$replace = array ('<p></p>');
echo preg_replace($search, $replace, $s);

I have also tried \n, \r, \n\r etc... in place of \n\n but to no avail.

Can anyone help me with this? I know text files aren't an ideal format
and a database would be easier, but the site will be used by people
with no computer knowledge what so ever, and it is easier to show them
how to use notepad  :¬)

Thanks in advance


Re: Carriage returns

On Wed, 21 Jun 2006 08:15:50 -0700, Dan Pearce wrote:
Quoted text here. Click to load it

Two things:  

1) I'd try "\r\n\r\n", Windows users (which I'd imagine your
no-computer-knowledge people are) using notepad inserts \r\n for a
carriage return.

2) Use double quotes instead of single quotes.  Try the following script
using PHP CLI:

print "Two blanks lines with double quotes:";
print "\n\n";
print "Two blanks lines with single quotes:";
print '\n\n';

$ php test2.php
Two blanks lines with double quotes:

Two blanks lines with single quotes:\n\n

\n's aren't interpreted within single quotes, so it searches for the
literal string slash-n not a newline char.

Quoted text here. Click to load it

I hope the above helps.  In future though, rather than guessing at line
endings using a hex editor may help ;-)



Andy Jeffries MBCS CITP ZCE   | gPHPEdit Lead Developer | PHP editor for Gnome 2 | Personal site and photos

Site Timeline