[eluser]waynhall[/eluser]
Thanks. First I put these functions at the top of my view:
Code:
<?php
function get_thumbnail($content) { // Returns src of first <img>
$start = strpos($content, '<img');
if($start !== FALSE) { // '!==' Because '0' evaluates to false
$end = strpos($content, '/>', $start) + 2;
$length = $end - $start;
$img_tag = substr($content, $start, $length);
if($start = strpos($img_tag, 'src="') ){
$start = $start + 5;
$end = strpos($img_tag, '" '); // assumes a space
$length = $end - $start;
$img_src = substr($img_tag, $start, $length);
return $img_src;
}
}
}
function get_excerpt($content) { // Returns contents of the first <p> tag
$start = stripos($content, '<p');
if($start !== FALSE) { // '!==' Because '0' evaluates to false
$p_open = strpos($content, '>', $start) + 1;
$p_close = strpos($content, '</p>', $p_open);
$length = $p_close - $p_open;
$p_contents = substr($content, $p_open, $length);
return $p_contents;
}
else return 'Excerpt not available';
}
?>
Then later, within the loop:
Code:
<?php
// if the post contains an image
if($thumbnail = get_thumbnail($post->content)) {
$img = '<img class="post-thumbnail" src="' . $thumbnail . '" />';
echo anchor($url, $img, array('title'=>$post->title));
}
?>
<?php
$excerpt = get_excerpt($post->content);
$continue = anchor($url, 'Continue reading "' . $post->title . '"', 'class="continue-reading"');
$excerpt .= $continue;
?>
<p class="excerpt">
<?php echo $excerpt; ?>
</p>
Although it's probably not good MVC to have these functions in the view. I'll work on moving these to private functions within the controller and passing the results along with the post array to the view.