TAGS :Viewed: 6 - Published at: a few seconds ago

[ Why are <br> tags duplicated when using the 'for' php function? ]

This code is supposed to help me in limiting the amount of characters seen in the timeline page. If the post is too long, (more than 400 characters), it should display a 'see more...' which should take the reader to a new page (posts.php?post=XXX). When I use this function, the second part after 'else' works fine. However, the first part seems to duplicate all the
tags, and the spaces in this page appear twice bigger than the ones in posts.php. I can't trace the problem, someone please help me.

function display_content($long_text, $link, $page='posts', $page_ext = 'post'){
    if(strlen($long_text) &gt;= 400){
        for($i=0; $i&lt;=400; $i++){
            echo nl2br($long_text[$i]);
        }
        echo "...&lt;br /&gt;&lt;a href='{$page}.php?{$page_ext}={$link}'&gt;Read more...&lt;/a&gt;";
    } else echo nl2br($long_text);
}// I have taken care of all security issues.

Answer 1


Instead of parsing all characters of your text :

   for($i=0; $i<=400; $i++){
        echo nl2br($long_text[$i]);
    }

Why don't you use :

echo nl2br(substr($long_text, 0, 400)); 

?