Question about printing newlines in text blocks (newbie)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
coolen
Forum Newbie
Posts: 19
Joined: Thu Aug 29, 2002 4:24 am
Location: Netherlands
Contact:

Question about printing newlines in text blocks (newbie)

Post by coolen »

Hi, I'm just starting in PHP.

I do have a problem with printing large textblock from a MYSQL database.

The text block contains dumps from buildlogs and should look like:


Configure:
- AIPSPATH not set and no explicit path is given
for: DMI, StationSim, CEP/CPA/OCTOPUSSY, CEP/CPA/UVD

Make all:
see configure.... could not compile packages DMI, StationSim, CEP/CPA/OCTOPUSSY, CEP/CPA/UVD

Make check:
see configure..


(this one was viewed in MySQL-Front)

However when I use the following code (as adviced in the books I use):

echo "<strong><br>Test Summary: </strong>";
echo htmlspecialchars( stripslashes($row["Summary"]));


it looks like :


Test Summary: Configure: - AIPSPATH not set and no explicit path is given for: DMI, StationSim, CEP/CPA/OCTOPUSSY, CEP/CPA/UVD Make all: see configure.... could not compile packages DMI, StationSim, CEP/CPA/OCTOPUSSY, CEP/CPA/UVD Make check: see configure..



so it looks like the newlines aren't printed.

Is there a solution for this ( I really like to have the newlines. Because thsi part was rather small, usually I have 100's of lines in that block.)


Thanx in advance
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You need to use the nl2br() function to convert the newlines to HTML line breaks.

Mac
coolen
Forum Newbie
Posts: 19
Joined: Thu Aug 29, 2002 4:24 am
Location: Netherlands
Contact:

Post by coolen »

:D Great, thnx, I'll have a look into it.
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

Simple:

Say you get the text block out of the DB and set it as the variable $text:

Code: Select all

$search = array("/\n/");
$replace = array("<br />\n");
$text = preg_replace($search, $replace, $text);
Then echo $text out, and it has line breaks!

later on, -Brian.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You really don't need to do all of the regular expression stuff, nl2br() is a built in function which will put HTML line breaks in where there are newlines.

Mac
coolen
Forum Newbie
Posts: 19
Joined: Thu Aug 29, 2002 4:24 am
Location: Netherlands
Contact:

Post by coolen »

Thanx both. Indeed nl2br() did the trick. So I am perfectly happy at the moment.

I don't doubt however that I will run into other questions shortly :)
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

:: sigh ::

I'll prolly end up using nl2br() :P...i forget where i got my snippet anyway...oh well, thanx twigletmac
Post Reply