...

View Full Version : Preview website Script



verbosity
02-22-2010, 04:52 PM
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?:confused:

mlseim
02-22-2010, 05:17 PM
"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).

verbosity
02-22-2010, 05:30 PM
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.

MattF
02-22-2010, 05:38 PM
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:



# 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;
}

/**********************************************************************/

verbosity
02-22-2010, 05:44 PM
Thanks, that hit the spot.

verbosity
02-22-2010, 06:21 PM
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?

MattF
02-22-2010, 06:35 PM
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:



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

verbosity
02-22-2010, 07:22 PM
I see. I was just thinking it was meant to be a variable or something. Thanks again!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum