Jump to content

Archived

This topic is now archived and is closed to further replies.

Mouse

Quoting the first 50 words… more>>

Recommended Posts

Mental Block time…
Quoting the first 50 words… more>>

Ok so it has something to do with count() but I can’t get my head around it… I want to quote the first 50 words of an article in the link to that page but how can I pull the first 50 words and put them along with a “more>>” link at the end???

Mouse

Share this post


Link to post
Share on other sites
preg_replace ("/([\w]{50})/", "$1 more\>\>", $text);

but I'm a bit rusty on reg expressions so check it first

Share this post


Link to post
Share on other sites
[!--quoteo(post=384101:date=Jun 15 2006, 09:03 AM:name=joquius)--][div class=\'quotetop\']QUOTE(joquius @ Jun 15 2006, 09:03 AM) [snapback]384101[/snapback][/div][div class=\'quotemain\'][!--quotec--]
preg_replace ("/([\w]{50})/", "$1 more\>\>", $text);

but I'm a bit rusty on reg expressions so check it first
[/quote]

i was thinking along the lines of

[code]
if(strlen($srchrow["desc"])>75){
          $srchrow["desc"]=substr($srchrow["desc"],0,75)."...";
        }
[/code]

but this counts letters not words... any ideas?

Mouse

Share this post


Link to post
Share on other sites
Prehaps [a href=\"http://uk.php.net/manual/en/function.str-word-count.php\" target=\"_blank\"]str_word_count[/a] may be what you're looking for?

Share this post


Link to post
Share on other sites
[code]
<?php
$MyString = "The move by Nielsen and sister company Netratings, upon whose research TV networks,
            Web sites and advertisers rely for setting advertising rates, could bring major changes to the TV
            industry as people increasingly watch shows outside the home or on computers, mobile devices and cell
            phones.
            The new testing, which also looks at viewership in restaurants and expands the use of electronic
            metering to smaller TV markets, could also dramatically change the way viewership figures are
            tallied and might result in major shifts in the way advertising dollars are spent and received.
            The new plan by Nielsen, which is owned by information provider VNU, is called Anytime Anywhere
            Media Measurement, or A2/M2. It will roll out over the next several years, starting with test
            programs as soon as this summer.";
$MyLink = "http://www.google.com";

function TrimStr($String, $Limit){
    $StringArr = explode(" ", $String);
    for($i=0; $i <= $Limit-1; $i++){
        $TrimStr .= $StringArr[$i] ." ";
    }
    return $TrimStr;
}

$ShortString = TrimStr($MyString, 50);

echo $ShortString ." <a href='". $MyLink ."' alt='More!'>More»</a>";
?>
[/code]

Usage should be easy to understand

Share this post


Link to post
Share on other sites
or...
[code]
//    get the first xx words for the teaser
$contents="some text";
$max_words=50;
if (count(explode(" ", $contents))> $max_words) {
$contents = substr($contents, 0, strnpos($contents, " ", $max_words));
}
$contents = $contents . "...More>"; [/code]

(part of ... [a href=\"http://www.nstoia.com/toh/technical/teasers/)\" target=\"_blank\"]http://www.nstoia.com/toh/technical/teasers/)[/a]

Lite...

Share this post


Link to post
Share on other sites
Most excelent... many thanks

Mouse [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.