Crop Content

Discussion in 'PHP' started by meannn, Jan 13, 2010.

  1. #1
    Hello, this snippet crops the content and puts "..." to the end. My question is, how can I get the rest of the content ?

    <?
    
    $str = "Yeah, but on my website, before I realized that converting quotes, etc, to entities was a bad idea, I already had a lot of stuff put into a database...Also, symbols like show up as question marks... and I don't really know how to fix that. In my four years of HTML experience, and two years of PHP experience, I've never been able to get character encoding to do what I want it to.";
    
    $chars = "150";
    
    function shortText($str, $chars)
    {
    if (strlen($str) > $chars)
        {
        $str = substr($str, 0, $chars);
        $str = $str . "...";
        return $str;
        }
    else
        {
    return $str;
        }
    }
    
    echo shortText($str, $chars);
    
    ?>
    PHP:
     
    meannn, Jan 13, 2010 IP
  2. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #2
    You mean the text which was leftout during the crop? Then something like this;
    $rest = substr($str, $chars, strlen($str));
    PHP:
     
    Sky AK47, Jan 13, 2010 IP
  3. astkboy2008

    astkboy2008 Peon

    Messages:
    211
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
  4. meannn

    meannn Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I just want to get the section which starts with "..." (script does this after 150 chars) and finishes at the end.
     
    meannn, Jan 13, 2010 IP
  5. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #5
    I don't get you, you want the text before the '...'? Then I assume you don't understand PHP?
    $str = substr($str, 0, $chars); //<----- This is the text before '...'
        $str = $str . "...";
        return $str;
    PHP:
     
    Sky AK47, Jan 13, 2010 IP
  6. meannn

    meannn Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    No man, I want the section which is after "..."
     
    meannn, Jan 13, 2010 IP
  7. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #7
    Can you please explain it clearly? From your previous posts I've understood either;
    - Get the text which is removed (which is supposed to be after ...)
    - Get the text before ... (which is already in the script)
     
    Sky AK47, Jan 13, 2010 IP
  8. meannn

    meannn Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    meannn, Jan 13, 2010 IP
  9. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #9
    Sky AK47, Jan 13, 2010 IP
  10. meannn

    meannn Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Okay thank you :). Sorry for my bad english, I am not english.
     
    meannn, Jan 13, 2010 IP
  11. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #11
    Here you go:

    I've modified your function, so if you set $show too 1 it will display the text from the defined character, if set to 0 it will just function as before (limit the text and add ...):

    <?php
      function shortText($str, $chars, $show)
        {
          if (strlen($str) > $chars)
            {
              $str2 = substr($str, $chars);
              $str = substr($str, 0, $chars);
              $str = $str . "...";
              
              if ($show == "1")
                {
                  echo $str2;
                }
              else
                {
                  return $str;
                }
            }
          else
            {
              return $str;
            }
        }
    
    
    $text = <<<TEXT
    Yeah, but on my website, before I realized that converting quotes, etc, to entities was a bad idea, I already had a lot of stuff put into a database...Also, symbols like show up as question marks... and I don't really know how to fix that. In my four years of HTML experience, and two years of PHP experience, I've never been able to get character encoding to do what I want it to.
    TEXT;
    
    $length = "150";
    
    //0 = don't show the text after the $length (150? characters)
    //1 = show the text after $length (150? characters)
    echo shortText($text, $length, "0");
    
    ?>
    PHP:
     
    danx10, Jan 13, 2010 IP
  12. meannn

    meannn Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Thanks danx. This snippet is nice, too.
     
    meannn, Jan 13, 2010 IP