Jump to content

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
Link to comment
https://forums.phpfreaks.com/topic/12051-quoting-the-first-50-words%E2%80%A6-more/
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
[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
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...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.