character_limiter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vikas1111
    New Member
    • Feb 2008
    • 122

    character_limiter

    Hi all,
    I want to print content of post but limited portion and after that i show a link “Read More” in end so that it leads to full post detail. But when i use substr() func and use 200 character it prints well but i need to print or need to end substring with full word

    example:

    Default:
    This is my post and when it mak…

    I need:
    This is my post and when it makes…

    I know there is some function in php but i really dont remember.

    thanks in advance:
  • vikas1111
    New Member
    • Feb 2008
    • 122

    #2
    This is one solution i got after little bit of Googling. I am looking for the function which can replace bellow lines of code.

    Code:
    $position=14; // Define how many characters you want to display.
    
    $message="gg rier j ajajjawer wjrwj rejwjre wjrwjre jwejr"; 
    $post = substr($message,$position,1); // Find what is the last character displaying. We find it by getting only last one character from your display message. 
    
    if($post !=" "){ // In this step, if last character is not " "(space) do this step . 
    
    // Find until we found that last character is " "(space) 
    // by $position+1 (14+1=15, 15+1=16 until we found " "(space) that mean character 20) 
    while($post !=" "){
    $i=1;
    $position=$position+$i; 
    
    $message="gg rier j ajajjawer wjrwj rejwjre wjrwjre jwejr"; 
    $post = substr($message,$position,1); 
    }
    
    }
    
    $post = substr($message,0,$position); // Display your message
    echo $post;
    echo "...";
    Last edited by Markus; Jul 22 '09, 09:50 AM. Reason: Added [code] tags.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      wouldn't it be easier to look for the first space after a specified length/offset?
      Code:
      // using a 50 chars minimum length
      $cut = strpos($text, ' ', 50);
      // now using $cut in substr()

      Comment

      • vikas1111
        New Member
        • Feb 2008
        • 122

        #4
        Originally posted by Dormilich
        wouldn't it be easier to look for the first space after a specified length/offset?
        Code:
        // using a 50 chars minimum length
        $cut = strpos($text, ' ', 50);
        // now using $cut in substr()
        Hi Dormilich,

        Thanks for reply. Yes its a good idea it works.

        Kind Regards

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Wouldn't it be better to cut the text of before reaching the limit?

          [code=php]
          $words = explode(" ", substr($post, 50));
          array_pop($word s);
          echo implode(" ", $words);
          [/code]
          I use this in a page where the text must not exceed the limit.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            it would be also possible to cut at 50 (or whatever value) chars and use strrpos() to find the last space.

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Originally posted by Dormilich
              it would be also possible to cut at 50 (or whatever value) chars and use strrpos() to find the last space.
              Good point, and that might even be more efficient then my array method.
              I'll have to look into that. Thanks! :-)

              Comment

              Working...