Jump to content


Photo

Quoting the first 50 words… more>>


  • Please log in to reply
6 replies to this topic

#1 Mouse

Mouse
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts
  • LocationToo Close to LONDON

Posted 15 June 2006 - 08:00 AM

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


#2 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 15 June 2006 - 08:03 AM

preg_replace ("/([\w]{50})/", "$1 more\>\>", $text);

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

#3 Mouse

Mouse
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts
  • LocationToo Close to LONDON

Posted 15 June 2006 - 08:27 AM

[!--quoteo(post=384101:date=Jun 15 2006, 09:03 AM:name=joquius)--][div class=\'quotetop\']QUOTE(joquius @ Jun 15 2006, 09:03 AM) View Post[/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

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

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

Mouse

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 15 June 2006 - 09:43 AM

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?

#5 Prismatic

Prismatic
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego

Posted 15 June 2006 - 10:04 AM

<?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>";
?>

Usage should be easy to understand

#6 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 15 June 2006 - 10:29 AM

or...
//    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>";

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

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#7 Mouse

Mouse
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts
  • LocationToo Close to LONDON

Posted 15 June 2006 - 11:35 AM

Most excelent... many thanks

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users