View Full Version : [PHP] file_get_contents
Icy Penguin
June 18th, 2007, 08:32 PM
Hi everyone,
Doing a site for which the homepage will have three main sections on it (e.g., about, donate, contact).
For those sections, there will be three other pages (about.htm, donate.htm, and contact.htm).
For the about section of the homepage, for example, I would like to have, say, the first 50 words from the div called "content-container" in "about.htm" and have those words written in the about section on the homepage, with like, an ellipsis at the end (...) and then a 'read more' link which links to about.htm
I was fooling around with file_get_contents but didn't really get anywhere (I suck at .PHP :D).
If anyone could help me out, that'd be great.
Thanks!
kdd
June 18th, 2007, 09:21 PM
http://us.php.net/file_get_contents
Each character is a byte, so where it says max_len, you can do 50 * avg. # of characters in each word...
That's rather a stupid idea. You should use some other function (scan through the list on that php site), which reads each word and increments your counter...
Icy Penguin
June 18th, 2007, 09:35 PM
Well - I think I can just use strlen or whatever and just substring to get the 50 characters - but how would I start at '<div id='container'> and end at the next '</div>'?
blazes
June 18th, 2007, 09:43 PM
http://www.php.net/filesize
Icy Penguin
June 18th, 2007, 10:43 PM
Thanks for your help. Managed to get it on my own using some basic brainstuff.
$the_file = $page . '.htm';
$file_contents = file_get_contents($the_file);
$search_haystack = htmlspecialchars($file_contents);
/////
$beginning_search_needle = '<div id="' . $div_id_name . '">';
$beginning_search_needle_length = strlen($beginning_search_needle);
$beginning_search_needle_position = strpos($search_haystack, $beginning_search_needle) + $beginning_search_needle_length;
$search_haystack = substr($search_haystack, $beginning_search_needle_position);
/////
$end_search_needle = '</div>';
$end_search_needle_length = strlen($end_search_needle);
$end_search_needle_position = strpos($search_haystack, $end_search_needle);
$search_haystack = substr($search_haystack, 0, $end_search_needle_position);
/////
$search_haystack = substr($search_haystack, 0, $content_length);
if($search_haystack{strlen($search_haystack)-1} == '.') {
$search_haystack = substr($search_haystack, 0, strlen($search_haystack)-1);
};
//////
$final_contents = $search_haystack;
$final_contents.= '... ' . '<a href="' . $the_file . '">' . 'Read More.' . '</a>';
echo $final_contents;
simplistik
June 18th, 2007, 11:52 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Word Truncate</title>
</head>
<body>
<?php
function truncate($content, $maxWords)
{
$contentArray = explode(' ',$content);
if(count($contentArray) > $maxWords && $maxWords > 0)
{
$content = implode(' ',array_slice($contentArray, 0, $maxWords)).'… <a href="#">Read More</a>';
}
return $content;
}
$maxWords = 50;
$content = file_get_contents('about.html');
echo truncate($content, $maxWords);
?>
</body>
</html>
about.html
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Fusce sagittis porttitor nisi. Duis a libero ac quam imperdiet consequat. Morbi porttitor cursus odio. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Suspendisse feugiat. Aliquam condimentum nisl non risus. Mauris eget tellus. Suspendisse ac massa. Sed sit amet nulla. Donec commodo. Nullam ullamcorper turpis facilisis turpis. In hac habitasse platea dictumst. Nunc sit amet metus vel metus consequat vulputate. Etiam eget nunc. Duis ante felis, dictum ac, laoreet a, aliquam vitae, elit. Donec ipsum sem, mattis vitae, accumsan ut, pulvinar ut, quam. Donec scelerisque. Quisque tortor ligula, dictum ut, fermentum eu, rhoncus quis, nulla. Morbi ut nisi. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
Icy Penguin
June 19th, 2007, 12:11 AM
Will try that out too simp - looks better than mine. :D Thanks
simplistik
June 19th, 2007, 09:18 AM
cool hope it works out for ya
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.