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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Preview website Script

    I know this must already exist with all the blogs and news scripts out there, but I can't seem to find one.

    I want to be able to post say the first paragraph and picture of a page and have a button that says read more.. or something of the like. The pages can have tables, links, and images.

    Anyone know a script I can tie in to what I already have?
    I thought about just saying end after the 300th word or something, but that wouldn't close any tags.

    By the time I add all the tags to check for and make close I figured I could find a script that does exactly what I want. Though this seems to be a harder to search for than I thought.

    Any ideas?

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    "The picture of the page" ....
    Do you mean a thumbnail snapshot of an online web page?

    You can only do that in REAL TIME if you have your own server.
    Otherwise, you'll have to do a screenshot (with a graphic editor) and make a thumbnail
    by hand, and upload that thumbnail (it won't be a real-time snapshot of a page).

  • #3
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I am running on my own server, but I don't want a screen shot.

    I want it kind of like a blog where you get a teaser amount of info that has a link that says read more. The teaser is usually the first paragraph or so.

    Kind of like www.lifehacker.com.

    But I don't need to change the format or anything.
    Last edited by verbosity; 02-22-2010 at 05:35 PM.

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by mlseim View Post
    Do you mean a thumbnail snapshot of an online web page?
    A truncation script to grab the first N characters of an entry, then link to the full article. Truncation and tag closing part:

    Code:
    # Truncate a string longer than maxchars length.
    
    function truncate($string, $maxchars)
    {
            if (strlen($string) <= $maxchars || strpos($string, '<code') !== false)
            {
                    return $string;
            }
            else if ($maxchars > 0)
            {
                    $tag1 = strpos($string, '<', ($maxchars-1));
                    $tag2 = strpos($string, '>', ($maxchars-1));
    
                    if (!$tag1 && !$tag2)
                    {
                            $maxchars = $maxchars;
                    }
                    else if ($tag1 < $tag2)
                    {
                            $maxchars = $tag1;
                    }
                    else if ($tag1 > $tag2)
                    {
                            $maxchars = ($tag2+1);
                    }
                    else
                    {
                            $maxchars = strlen($string);
                    }
    
                    $string = substr($string, 0, $maxchars);
                    $string = close_html_tags($string);
            }
            return $string;
    }
    /**********************************************************************/
    
    # Close any open html tags when truncating a post.
    
    function close_html_tags($string)
    {
            $selfclose = array('base', 'br', 'hr', 'img', 'link', 'meta', 'param');
    
            if (preg_match_all('/<([a-z0-9]{1,}).*?>/', $string, $start_tags))
            {
                    $start_tags = $start_tags[1];
    
                    if (preg_match_all('/<\/([a-z0-9]{1,}).*?>/', $string, $end_tags))
                    {
                            $open_tags = array();
                            $end_tags = $end_tags[1];
    
                            foreach ($start_tags as $key => $tag)
                            {
                                    $tagpos = array_search($tag, $end_tags);
    
                                    if (is_integer($tagpos))
                                    {
                                            unset($end_tags[$tagpos]);
                                    }
                                    else if (!in_array($tag, $selfclose))
                                    {
                                            $open_tags[] = $tag;
                                    }
                            }
                    }
                    else
                    {
                            $open_tags = $start_tags;
                    }
    
                    $count = 0;
                    $tag_count = count($open_tags);
                    $open_tags = array_reverse($open_tags);
    
                    while ($count < $tag_count)
                    {
                            if (!in_array($open_tags[$count], $selfclose))
                            {
                                    $open_tags[$count] = str_replace(array('<', '>'), '', $open_tags[$count]);
                                    $string .= '</'.$open_tags[$count].'>';
                            }
                            ++$count;
                    }
            }
            return $string;
    }
    
    /**********************************************************************/
    Last edited by MattF; 02-22-2010 at 05:44 PM.

  • Users who have thanked MattF for this post:

    verbosity (02-22-2010)

  • #5
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks, that hit the spot.

  • #6
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    PHP Code:
    strpos($string'<code') !== false 
    I'm not sure what this is achieving? Err. know it is looking for "<code" but why would that be in there?

  • #7
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by verbosity View Post
    PHP Code:
    strpos($string'<code') !== false 
    I'm not sure what this is achieving? Err. know it is looking for "<code" but why would that be in there?
    So that it leaves any content with a code block untouched. You can change that line to just:

    Code:
    if (strlen($string) <= $maxchars)

  • #8
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I see. I was just thinking it was meant to be a variable or something. Thanks again!


  •  

    Posting Permissions

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