Page 1 of 1

Limit forum index page characters & line breaks

Posted: Thu Mar 14, 2013 5:33 pm
by Alpal
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

Re: Limit forum index page characters & line breaks

Posted: Thu Mar 14, 2013 6:39 pm
by requinix
How do you want to solve this? Not show any line breaks? Stop at the first line break if it comes before the 150 character limit?

Re: Limit forum index page characters & line breaks

Posted: Thu Mar 14, 2013 6:55 pm
by Alpal
Stop at the first line break if it comes before the 150 character limit

Re: Limit forum index page characters & line breaks

Posted: Thu Mar 14, 2013 8:13 pm
by requinix
Then I think I would
1. Start with the string including the <p> tags.
2. If it starts with a <p>, grab everything between it and the </p> and operate on that instead.
3. Cut the string.

Re: Limit forum index page characters & line breaks

Posted: Fri Mar 15, 2013 12:06 am
by Alpal
OK
So currently we
1. display the whole string if less than 150 characters
2. cut the string at a space if the string is greater than 150 characters

Your suggestion is we to cut the string if a </p> occurs before 150 characters
How do I do that?
Sorry, while I have a general understanding of php, my ability to write php is not good

As always, thank you in advance for any assistance you may offer

Re: Limit forum index page characters & line breaks

Posted: Fri Mar 15, 2013 1:19 am
by requinix
Instead of working on the original string itself, put it into a temporary variable and use that in #2. Still works the same way, right?

Now insert a step in between those. In this step you check if the string in the new variable starts with "<p>" (you'll probably want to do it case-insensitively): if so then grab the portion up to the "</p>" and save that back into the same variable. Then step #3 will work on that new string...

Re: Limit forum index page characters & line breaks

Posted: Fri Mar 15, 2013 1:27 am
by Alpal
Sounds great, wish I knew what you are talking about

Re: Limit forum index page characters & line breaks

Posted: Fri Mar 15, 2013 2:44 am
by requinix
If you haven't noticed I'm trying to avoid simply giving you the precise code and instead get you to understand how it works.

It's running a string through a sequence of steps. Transformations, if you will. After these transformations you output it.
You start with the original string, transform it by dealing with <p>s if appropriate, transform it by cutting it to 150 characters if appropriate, and output.

Code: Select all

$string = "original string"
if $string starts with "<p>" {
    $string = part between the <p> and the </p>
}
if $string is longer than 150 characters {
    $string = everything up to the 150th character, then up to the space (word boundary) after
}
output $string

Re: Limit forum index page characters & line breaks

Posted: Fri Mar 15, 2013 11:29 pm
by Alpal
Thank you for pushing me to investigate.
Came up with this after a lot of research, please let me know if ok. It works!

<?php

//explode the text (string) into sections that end with </p>
$text = $string. $row_forum_topic["post_content"];

$exptext = explode("</p>", $text);


if(strlen($exptext[0]) < 150)
{
echo $exptext[0];

}
// if exploded text exceeds 150 characters break at a space

elseif(strlen($exptext[0]) > 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 "...";

}

?>

Spent a lot of time looking at how to break text at the </p> tag. Looked at all sorts of methods and then found the "Explode" method that breaks the text into blocks depending on what you look for, in my case I was looking for the </p> tag.
After breaking the text up I then only needed to look at the first paragraph and if it was <150 characters display it. If it was > 150 characters then break it at the end of a word and a space.

The only thing I can see that might upset the way this displays is a <br/> tag, tried to write this in but could not get it to work?.

Not sure if I have written it correctly but it does work. My ability to write php is very limited. Really need to educate myself, can you suggest any books on php?

Sorry, am working in a different time zone in Australia.

Really appreciate your assistance