Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder GSimpson's Avatar
    Join Date
    Aug 2006
    Location
    New Zealand
    Posts
    268
    Thanks
    9
    Thanked 0 Times in 0 Posts

    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.

  • #2
    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
    How about substr() instead?

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

    0, 1500 would display only the first 1500 characters.




    ---
    Leonard Whistler

  • #3
    Regular Coder GSimpson's Avatar
    Join Date
    Aug 2006
    Location
    New Zealand
    Posts
    268
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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.

  • #4
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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); 

  • #5
    New Coder
    Join Date
    May 2009
    Posts
    75
    Thanks
    1
    Thanked 12 Times in 12 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •