Limit forum index page characters & line breaks
Posted: Thu Mar 14, 2013 5:33 pm
Hi, have built a small forum. It works well but have a small problem on the index page.
Purely for aesthetic reasons I would like to limit the visible portion of a new post to 150 characters, have this code which does the job ....
<?php
if(strlen($row_forum_topic['post_content']) < 150)
{
echo ($row_forum_topic['post_content']);
}
elseif(strlen($row_forum_topic['post_content']) > 150)
{
$position=150; // Define how many characters you want to display.
$message=$row_forum_topic['post_content'];
// Find what is the last character.
$post = substr($message,$position,1);
// In this step, if the last character is not " "(space) run this code.
// Find until we found that last character is " "(space)
// by $position+1 (14+1=15, 15+1=16 until we found " "(space) that mean character no.20)
if($post !=" "){
while($post !=" "){
$i=1;
$position=$position+$i;
$message=$row_forum_topic['post_content'];
$post = substr($message,$position,1);
}
}
$post = substr($message,0,$position);
// Display your message
echo $post;
echo "...";
}
?>
PROBLEM
If the person inserting the post puts a line break before the 150 character limit then it will display everything until the 150 character limit is reached. Am using a text editor when the post is inserted so there is some html available. When you are posting if you hit enter it closes the </p> tag and inserts a new <p> tag
eg
<p>this is my post</p>
<p>I am writing</p>
I found the above script online and adapted it to suit my purpose, my php knowledge is not sufficient to adapt it further.
As always, thank you in advance for any assistance you may offer
Purely for aesthetic reasons I would like to limit the visible portion of a new post to 150 characters, have this code which does the job ....
<?php
if(strlen($row_forum_topic['post_content']) < 150)
{
echo ($row_forum_topic['post_content']);
}
elseif(strlen($row_forum_topic['post_content']) > 150)
{
$position=150; // Define how many characters you want to display.
$message=$row_forum_topic['post_content'];
// Find what is the last character.
$post = substr($message,$position,1);
// In this step, if the last character is not " "(space) run this code.
// Find until we found that last character is " "(space)
// by $position+1 (14+1=15, 15+1=16 until we found " "(space) that mean character no.20)
if($post !=" "){
while($post !=" "){
$i=1;
$position=$position+$i;
$message=$row_forum_topic['post_content'];
$post = substr($message,$position,1);
}
}
$post = substr($message,0,$position);
// Display your message
echo $post;
echo "...";
}
?>
PROBLEM
If the person inserting the post puts a line break before the 150 character limit then it will display everything until the 150 character limit is reached. Am using a text editor when the post is inserted so there is some html available. When you are posting if you hit enter it closes the </p> tag and inserts a new <p> tag
eg
<p>this is my post</p>
<p>I am writing</p>
I found the above script online and adapted it to suit my purpose, my php knowledge is not sufficient to adapt it further.
As always, thank you in advance for any assistance you may offer