Jump to content

Readmore


unidox

Recommended Posts

I'd probably just show the first few words of a given article, which could be achieved by exploding by a space, and echoing the first few words. For example:

<?php
$str = 'Here is my news arcticle that i only want to show the first few words of';
$num_words = 3;
$words = explode(' ',$str);
$i = 0;
$teaser = '';
while($i<$num_words){
$teaser .=' '.$words[$i];
$i++;
}
echo $teaser;
?>

 

You'd then include a link after that to read the whole article.

Link to comment
https://forums.phpfreaks.com/topic/70780-readmore/#findComment-355876
Share on other sites

that is a nasty idea to explode that big string, instead way better idea is find the first  punctuation mark that falls greater than X characters in (like 10ish).  Then just echo out a cut string from 0-that mark, unless that mark is greater than Y characters (say y is 25ish) then it just shows the first Y characters. By exploding you going to make a lot of useless data.  Exploding this post alone is like a 50 item array, vs a test for that comma, and then simply displaying that portion up to it.

Link to comment
https://forums.phpfreaks.com/topic/70780-readmore/#findComment-355899
Share on other sites

Um, darkfreaks $str would have to be an integer for that to be true.

 

strlen() would do what darkfreaks is showing.

 

<?php
$string = "i like to spam chats and have fun while randomly typing sentences using only my pointer fingers can this sentence have any more run ons?";

if(strlen($string) > 100){
$dot = " ...";
}else {
$dot = "";
}

echo substr($string,0,100) . $dot;
?>

 

Will show:

 

i like to spam chats and have fun while randomly typing sentences using only my pointer fingers can ...

Link to comment
https://forums.phpfreaks.com/topic/70780-readmore/#findComment-355970
Share on other sites

Archived

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

×
×
  • 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.