Jump to content

Recommended Posts

So I can't find anything that really does what I want it to do. I have a blog that I made. I'm trying to keep my home page rather short, but want to give them the option of expanding the entry without changing pages. So what I'm doing is using the "read more" link with css to change its display (display:none;).

 

I have that part working though! I click "Read More" and it shows the rest of my entry. I click "View Less" and it hides it.

 

  $showcount = 500;
  $hidecount = $charcount - $showcount;
  $content_show = substr($post['content'], 0, $showcount);
  $content_hide = substr($post['content'], $showcount, $hidecount);

 

My problem. I'm using the above code to pull the part that always shows and the part that's temporarily hidden. But sometimes.. it cuts off letters mid-word. Like..

 

Hello! My name is Austin. Ni[hidden]ce to meet you![/hidden]

 

How can I prevent that?

 

Edit: Or better yet.. is there a way I can make it so it does the split after three paragraphs or something?

Link to comment
https://forums.phpfreaks.com/topic/139618-solved-expand-post-ie-read-more/
Share on other sites

For the paragraphs, you could just cut if off after the 3rd \n given that your DB has the raw data. So this would work:

 

  $content = split("\n", $post['content']);
  $i=0;
  foreach ($content as $value) {
      if ($i == 2) {
          $content_show .= $value;
      }else {
          $content_hide .= $value;
      }
      $i++;
  }

 

Should do it. If you want to do the substr method I found this on the php.net site under substr

 

<?php
/*
    An advanced substr but without breaking words in the middle.
    Comes in 3 flavours, one gets up to length chars as a maximum, the other with length chars as a minimum up to the next word, and the other considers removing final dots, commas and etcteteras for the sake of beauty (hahaha).
   This functions were posted by me some years ago, in the middle of the ages I had to use them in some corporations incorporated, with the luck to find them in some php not up to date mirrors. These mirrors are rarely being more not up to date till the end of the world... Well, may be am I the only person that finds usef not t bre word in th middl?

Than! (ks)

This is the calling syntax:

    snippet(phrase,[max length],[phrase tail])
    snippetgreedy(phrase,[max length before next space],[phrase tail])

*/

function snippet($text,$length=64,$tail="...") {
    $text = trim($text);
    $txtl = strlen($text);
    if($txtl > $length) {
        for($i=1;$text[$length-$i]!=" ";$i++) {
            if($i == $length) {
                return substr($text,0,$length) . $tail;
            }
        }
        $text = substr($text,0,$length-$i+1) . $tail;
    }
    return $text;
}

// It behaves greedy, gets length characters ore goes for more

function snippetgreedy($text,$length=64,$tail="...") {
    $text = trim($text);
    if(strlen($text) > $length) {
        for($i=0;$text[$length+$i]!=" ";$i++) {
            if(!$text[$length+$i]) {
                return $text;
            }
        }
        $text = substr($text,0,$length+$i) . $tail;
    }
    return $text;
}

// The same as the snippet but removing latest low punctuation chars,
// if they exist (dots and commas). It performs a later suffixal trim of spaces

function snippetwop($text,$length=64,$tail="...") {
    $text = trim($text);
    $txtl = strlen($text);
    if($txtl > $length) {
        for($i=1;$text[$length-$i]!=" ";$i++) {
            if($i == $length) {
                return substr($text,0,$length) . $tail;
            }
        }
        for(;$text[$length-$i]=="," || $text[$length-$i]=="." || $text[$length-$i]==" ";$i++) {;}
        $text = substr($text,0,$length-$i+1) . $tail;
    }
    return $text;
}

/*
echo(snippet("this is not too long to run on the column on the left, perhaps, or perhaps yes, no idea") . "<br>");
echo(snippetwop("this is not too long to run on the column on the left, perhaps, or perhaps yes, no idea") . "<br>");
echo(snippetgreedy("this is not too long to run on the column on the left, perhaps, or perhaps yes, no idea"));
*/
?>

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.