Reproducing line breaks

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Reproducing line breaks

Post by Noobie »

Hi

After a lot of swearing, head-banging and generally petulant behaviour on my part I've finally sort of got my news archive working.

I've got a mysql db which the site owner can enter news items into from a form on their website.

The news item heading and date are displayed on one page and when clicked the heading takes the user to the full new item on a new page.

My main problem now is that the line breaks entered by the site owner when they enter an item in the submit form aren't preserved in the reproduced full article.

I tacked on a few lines of code I borrowed from a book which I thought might help but don't appear to do anything. I'd be really grateful if you could have a look at my code and offer any suggestions.

I checked the DB in phpadmin and the line breaks are preserved there - just not in the reproduced version.

Code: Select all

<?php

    $dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
    mysql_select_db ("mydatabase");
 			
    // get the id from the URL request
    $id = $_REQUEST['id'];

    // retrieve the row from the database
    $query = "SELECT * FROM archive WHERE id='$id'";
   
    $result = mysql_query( $query );

    // print out the results
    if( $result && $newsitem = mysql_fetch_object( $result ) )
    {
      // print out the info
      $newsdate = $newsitem -> newsdate;
      $newssummary = $newsitem -> newssummary;
      $newstext = $newsitem -> newstext;

      echo '<p>' . $newsdate . '<br />' . '<strong>' . 
			$newssummary . '</strong>' . '<br />' . $newstext . '</p>';
			}
			
                        // special characters
			$newssummary = htmlspecialchars($newssummary);
			$newstext = htmlspecialchars($newstext);
                        
                        // paragraphs and line breaks
			$newstext = ereg_replace("\r\n", "\n", $newstext);
			$newstext = ereg_replace("\r", "\n", $newstext);
			$newstext = ereg_replace("\n\n", '</p><p>', $newstext);
			$newstext = ereg_replace("\n", '<br />', $newstext);
			
                        // hyperlinks
			$newstext = eregi_replace (
			 '\\[L]([-_./a-z0-9!&%#?+,\'=:;@~]+)\\[EL]',
			 '<a href="\\1">\\2</a>', $newstext);
 ?>
Thanks in advance for any suggestions.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

consider using nl2br() instead all those ereg's
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Post by Noobie »

Hi n00b Saibot

(Please bear with me on this because I'm only just getting to grips with all this)

I replaced the eregs with this:

Code: Select all

function br2nl($str) {
      $str = preg_replace("/(\r\n|\n|\r)/", "", $str);
      return preg_replace("=<br */?>=i", "\n", $str);
      }
but still don't see any line breaks appearing in the news stories - not sure what I'm doing wrong...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Just use nl2br($string) to convert the \n characters to <br />'s.

There's not usually a reason to go from <br /> back to \n but it can be done yes.
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Post by Noobie »

Hi

Thanks for the reply.

I've been fiddling around trying to get it to work - unfortunately I don't know enough to know what I'm doing wrong :?

I've currently got this:

Code: Select all

...

if( $result && $newsitem = mysql_fetch_object( $result ) )
    {
      // print out the info
      $newsdate = $newsitem -> newsdate;
      $newssummary = $newsitem -> newssummary;
      $newstext = $newsitem -> newstext;

      echo '<p>' . $newsdate . '<br />' . '<strong>' . 
			$newssummary . '</strong>' . '<br />' . $newstext . '</p>';
			}
			
			// special characters
			$newssummary = htmlspecialchars($newssummary);
			$newstext = htmlspecialchars($newstext);
      			
			// hyperlinks
			$newstext = eregi_replace (
			 '\\[L]([-_./a-z0-9!&%#?+,\'=:;@~]+)\\[EL]',
			 '<a href="\\1">\\2</a>', $newstext);
      
      function br2nl($newstext) {
      $newstext = preg_replace("/(\r\n|\n|\r)/", "", $newstext);
      
      }

.....
But still no line breaks.

Sorry to be so dense!
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

all that is not needed! you have to use only

Code: Select all

$newstext = nl2br($newstext);
i.e. function nl2br is pre-defined. click the link I posted above to know more...
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Post by Noobie »

Hi again n00b Saibot

I had looked at that page (and tried some of the code bits too) but they just seemed to disagree with each other about which was the best method and I didn't know enough to understand which bits of code were useful and which were unnecessary!

Anyway - I've got it working now thanks to you. I had tried the line you suggested - but at the end of the script which didn't seem to work. I moved it around and eventually found the right place for it and hey presto it now works.

Thank you for your patience! :D
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Noobie wrote:Hi again n00b Saibot

I had looked at that page (and tried some of the code bits too) but they just seemed to disagree with each other about which was the best method and I didn't know enough to understand which bits of code were useful and which were unnecessary!

Anyway - I've got it working now thanks to you. I had tried the line you suggested - but at the end of the script which didn't seem to work. I moved it around and eventually found the right place for it and hey presto it now works.

Thank you for your patience! :D
We all gotta start somewhere :D Good username choice ;)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

d11wtq wrote:Just use nl2br($string) to convert the \n characters to <br />'s.

There's not usually a reason to go from <br /> back to \n but it can be done yes.
nl2br doesn't actually convert, all it does is insert <br /> infront of the line break :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Jenk wrote:
d11wtq wrote:Just use nl2br($string) to convert the \n characters to <br />'s.

There's not usually a reason to go from <br /> back to \n but it can be done yes.
nl2br doesn't actually convert, all it does is insert <br /> infront of the line break :)
You're quite right Jenk! :)
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Post by Noobie »

d11wtq wrote:
We all gotta start somewhere :D Good username choice ;)
:lol: Yeah well it's accurate! I'm reasonably well experienced in other areas of web design - CSS, XHTML - even managing to use PHP includes with reasonable results. But Databases and how PHP interacts with them is a completely new area to me (been avoiding it ;).

It does everyone good to be a Noobie at something occasionally - prevents cockiness setting in!
Post Reply