Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-07-2009, 06:53 AM   PM User | #1
GSimpson
Regular Coder

 
GSimpson's Avatar
 
Join Date: Aug 2006
Location: New Zealand
Posts: 268
Thanks: 9
Thanked 0 Times in 0 Posts
GSimpson is an unknown quantity at this point
Question Stop posting tag.

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.
__________________
The internet is my Sandbox, and notepad is my Spade n' Bucket.
GSimpson is offline   Reply With Quote
Old 06-07-2009, 06:59 AM   PM User | #2
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
How about substr() instead?

PHP Code:
echo substr('abcdef'04);  // abcd 

0, 1500 would display only the first 1500 characters.




---
__________________
Leonard Whistler
Len Whistler is offline   Reply With Quote
Old 06-07-2009, 07:54 AM   PM User | #3
GSimpson
Regular Coder

 
GSimpson's Avatar
 
Join Date: Aug 2006
Location: New Zealand
Posts: 268
Thanks: 9
Thanked 0 Times in 0 Posts
GSimpson is an unknown quantity at this point
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.
__________________
The internet is my Sandbox, and notepad is my Spade n' Bucket.
GSimpson is offline   Reply With Quote
Old 06-07-2009, 03:07 PM   PM User | #4
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
Using the [trim] tag and assuming that the variable $string points to the string to be trimmed:

PHP Code:
echo preg_replace('/(.*?)\[trim\].*/'"$1"$string); 
venegal is offline   Reply With Quote
Old 06-08-2009, 02:28 AM   PM User | #5
TheBlueblib
New Coder

 
Join Date: May 2009
Posts: 74
Thanks: 1
Thanked 12 Times in 12 Posts
TheBlueblib is an unknown quantity at this point
Quote:
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
PHP Code:
$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:
PHP Code:
$total 0;//Set the variable which will growth with each blog post added
$blog_counter=1;
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
$blog_counter++;


Last edited by TheBlueblib; 06-08-2009 at 02:31 AM..
TheBlueblib is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:33 AM.


Advertisement
Log in to turn off these ads.