Karakas Online

18.7.1. How to get rid of the need to use <br> for new lines

It is one of the most annoying requirements for users who want to submit a News article to your site: they have to write the text either in BBcode, or in HTML. While BBcode is easy to learn and use, HTML can be a real pain for the average Jo user. Worse yet, for sites that have disabled BBcode, HTML is the only available language for the aspiring journalist. Thus, a lot of people find themselves forced to write their articles with the editor of their choice, then export that document to HTML with the editor's own export function. Although this scenario will not alleviate the need for a subsequent scrutiny of the HTML code thus produced, for the purpose of locating and eliminating the HTML tags that are not allowed by the PHP-Nuke site (see Section 16.1), it definitely has its merits, especially for long text.

Those users however, who will rather opt for a quick entering of the text directly in the form fields, will most often be reminded the hard way, by a text totally lacking paragraph structure, that something is missing from their text: two break tags (<br><br>) for the insertion of a blank line, something that is used intuitively to separate paragraphs. Even seasoned HTML coders will find themselves routinely omitting the obligatory <br> tags, so a way to avoid the need for them is great help for both beginners and specialists alike.

To eliminate the <br> plague once for all, you only have to enter a 2-liner in the modules/news/index.php file. Find the lines:

        getTopics($s_sid);
        formatTimestamp($time);
        $subject = stripslashes($subject);
        $hometext = stripslashes($hometext);
        $notes = stripslashes($notes);
        $introcount = strlen($hometext);
        $fullcount = strlen($bodytext);
        $totalcount = $introcount + $fullcount;
        $c_count = $comments;
        $r_options = "";

and append the following two ones to them:

        $hometext=nl2br("$hometext");
        $bodytext=nl2br("$bodytext");

Then, in the modules/News/article.php, find the lines

if ($notes != "") {
    $notes = "<br><br><b>"._NOTE."</b> <i>$notes</i>";
} else {
    $notes = "";
}
if($bodytext == "") {
    $bodytext = "$hometext$notes";
} else {
    $bodytext = "$hometext<br><br>$bodytext$notes";
}

and add in 2 more lines, like this:

$hometext = nl2br($hometext);
$bodytext = nl2br($bodytext);

The nl2br PHP function (newline to break) will automatically insert HTML <br /> tags (breaks) before every newline in the short ($hometext) and extended text ($bodytext) of the news article (see also How do I stop using the br tag, Linebreaks when entering text).

Last updated Sun Aug 7 21:14:12 CEST 2005 Permalink: http://www.karakas-online.de/EN-Book/get-rid-of-br-in-news.html All contents © 2004 Chris Karakas, Claudio Erba