3

I am working on my php website (Not a Wordpress site) on the main index I display the two newest post. The thing is on the description it shows the entire article I find myself needing to display post excerpts maybe 35 word limit.

<?=$line["m_description"]?>

<?
$qresult3 = mysql_query("SELECT * FROM t_users WHERE u_id=".$line["m_userid"]." LIMIT 1");
if (mysql_num_rows($qresult3)<1) { ?>
user1167384
  • 31
  • 1
  • 2
  • 1
    What’s your question? You’d like to know how to only show 35 words of the article? – Zoe Edwards Jan 24 '12 at 16:08
  • Yes I would like to know how to only show 35 words of the article. – user1167384 Jan 24 '12 at 16:09
  • possible duplicate of [How to capture complete words using substr() in PHP, limit by word?](http://stackoverflow.com/questions/3538130/how-to-capture-complete-words-using-substr-in-php-limit-by-word) –  Jan 24 '12 at 16:10
  • possible duplicate of [Making sure PHP substr finishes on a word not a character](http://stackoverflow.com/questions/1233290/making-sure-php-substr-finishes-on-a-word-not-a-character) –  Jan 24 '12 at 16:11
  • And so on ... you get the point ... –  Jan 24 '12 at 16:12
  • 1
    Your question title doesn't reflect the real question and, as @rdlowrey is driving at, this question has been answered quite a bit. – Herbert Jan 24 '12 at 16:23
  • Showing an excerpt is not the same as finishing on a word boundary or limit by word. That's a big chunk of it, but there are other caveats and issues. The question could be a bit more detailed and have some examples of the input and expected output, but it's a valid question. – Kato Jan 24 '12 at 16:37
  • Sorry about that guys, what I'm trying to do is limit by word count. Thanks for your time & help but still can't figure this out smh. – user1167384 Jan 24 '12 at 16:43
  • This is the code that displays that article text `=$line["m_description"]?>` So all I'm trying to do is limit the word count to 35 words. – user1167384 Jan 24 '12 at 16:49

2 Answers2

9
<?php

// just the excerpt
function first_n_words($text, $number_of_words) {
   // Where excerpts are concerned, HTML tends to behave
   // like the proverbial ogre in the china shop, so best to strip that
   $text = strip_tags($text);

   // \w[\w'-]* allows for any word character (a-zA-Z0-9_) and also contractions
   // and hyphenated words like 'range-finder' or "it's"
   // the /s flags means that . matches \n, so this can match multiple lines
   $text = preg_replace("/^\W*((\w[\w'-]*\b\W*){1,$number_of_words}).*/ms", '\\1', $text);

   // strip out newline characters from our excerpt
   return str_replace("\n", "", $text);
}

// excerpt plus link if shortened
function truncate_to_n_words($text, $number_of_words, $url, $readmore = 'read more') {
   $text = strip_tags($text);
   $excerpt = first_n_words($text, $number_of_words);
   // we can't just look at the length or try == because we strip carriage returns
   if( str_word_count($text) !== str_word_count($excerpt) ) {
      $excerpt .= '... <a href="'.$url.'">'.$readmore.'</a>';
   }
   return $excerpt;
}

$src = <<<EOF
   <b>My cool story</b>
   <p>Here it is. It's really cool. I like it. I like lots of stuff.</p>
   <p>I also like to read and write and carry on forever</p>
EOF;

echo first_n_words($src, 10);

echo "\n\n-----------------------------\n\n";

echo truncate_to_n_words($src, 10, 'http://www.google.com');

EDIT: Added functional example and accounted for punctuation and numbers in text

Kato
  • 40,352
  • 6
  • 119
  • 149
0

I have a function though other people may say it's not good because I'm still good at PHP too (tips welcome people) but this will give you what you are looking for, it may need better coding if anyone has suggestions.

function Short($text, $length, $url, $more){
$short = mb_substr($text, 0, $length);

if($short != $text) {
    $lastspace = strrpos($short, ' ');
    $short = substr($short , 0, $lastspace);

    if(!$more){
        $more = "Read Full Post";
    } // end if more is blank

    $short .= "...[<a href='$url'>$more</a>]";
} // end if content != short

$short = str_replace("’","'", $short);
$short = stripslashes($short);
$short = nl2br($short);

} // end short function

To Use:

say your article content is the variable $content

function($content, "35", "http://domain.com/article_post", "Read Full Story");
echo $short;

Similarly, you can adjust the function to remove $url and $more from it and just have the excerpt with ... at the end.

bowlerae
  • 924
  • 1
  • 14
  • 37
  • @bowlerae this looks like an excellent starting point. There are more symbols than simply single quotes to consider if you want to clean up this sort of funky windows character (hyphens, smart/double quotes, dashes, copy symbols, etc), but that's a whole topic unto itself. Also, excerpts almost always have trouble with html that ends up truncated by the end; solve that too! :) – Kato Jan 24 '12 at 16:57