View Full Version : Stop posting tag.

06-07-2009, 07:53 AM
Hi there,

I've been working on a cms/blog system and need some help with a feature I would like to imply. Okay, so on the homepage I have the last 5 articles displaying one after the other. Currently, they trim the string down to 1500 characters so that the homepage doesn't display the entire artcle as some are quite lengthy.

My input is a textarea supporting a custom bbcode that parses to html for insertion to the database then displays on the homepage as that html. I would like to create a tag that lets me stop displaying from that point onwards, so in other words, only displays up until the first occourence of [trim] and doesn't display the tag "[trim]" itself.

I thought strstr() might help but it displays from [trim] onwards.

Thanks in advance.

Len Whistler
06-07-2009, 07:59 AM
How about substr() instead?

echo substr('abcdef', 0, 4); // abcd

0, 1500 would display only the first 1500 characters.


06-07-2009, 08:54 AM
No no, that's what I'm already doing to trim it to 1500 characters - I need to be able to adjust the length of the article displayed on the homepage based on the articles length/content. Sometimes, as it parses youtube and images, it cuts out half way through the coding, so I need to create a tag that it reads everything up until.

06-07-2009, 04:07 PM
Using the [trim] tag and assuming that the variable $string points to the string to be trimmed:

echo preg_replace('/(.*?)\[trim\].*/', "$1", $string);

06-08-2009, 03:28 AM
Sometimes, as it parses youtube and images, it cuts out half way through the coding

I ran into the same problem myself, I'll try and explain how I fixed it. I was using a while loop to display a certain amount of blog posts, so each cycle of the loop is a blog post with title, body etc. Each time the loop passed it would add the length of the body to a variable, like
$total = $total + strlen($blog_body); This variable $total would accumulate each time a blog post was added, and as soon as it passed a certain limit (e.g. 1500) I told it to break the while loop and hence no more blog posts would be added, but none of them would be cut off mid sentence either.

So if you're using a while loop (which you most likely are), then try adding something like this:

$total = 0;//Set the variable which will growth with each blog post added
while ($blog_counter<=5) {//show a maximum of 5 blog posts no matter what
if ($total > 1500) {
break;//Stop display blog posts if maximum string length is exceeded, 1500 in this case

//code for display blog content using $blog_counter to identify which blog post goes here

$total = $total + strlen($blog_body);//increment the string length to the $total variable