[PHP] Truncate String

Results 1 to 10 of 10
  1. #1
    Web Developer Markshall is offline
    MemberRank
    Oct 2009 Join Date
    EnglandLocation
    628Posts

    [PHP] Truncate String

    I know this is very basic, but it seems to be a common question that people ask me: "how to truncate a string in PHP" -- basically, how do I cut a string after a certain length?

    Well it's simple, I created a function for you all to use if you wish that will cut a string off after a certain length that you choose.

    The usage is very easy and is demonstrated in the code snippet below:

    PHP Code:
    <?php
    function truncate$str$allowed_length=10 )
    {
        return 
    substrtrim$str ), 0$allowed_length ); //return the truncated value
    }

    echo 
    truncate"Mark Eriksson is the best person ever!" ); //output: Mark Eriks
    echo truncate"Mark Eriksson is the best person ever!"25 ); //output: Mark Eriksson is the best
    ?>
    I hope that this helps you if you choose to use it.

    Mark.


  2. #2
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,098Posts

    Re: [PHP] Truncate String

    Why do you use trim? (I know what it does, wanna know 'why' you think I don't want white-space?)

  3. #3
    Fuck. SheenBR is offline
    ModeratorRank
    Feb 2008 Join Date
    Jaú, BrazilLocation
    2,433Posts

    Re: [PHP] Truncate String

    would be nice if you add the white-spaces removal like this:

    PHP Code:
    function truncate$str$allowed_length=10,$remove_spaces=false 
    false by default

  4. #4
    Account Upgraded | Title Enabled! AngraMainyu is offline
    MemberRank
    May 2011 Join Date
    445Posts

    Re: [PHP] Truncate String

    Your code wraps substr() by supplying two of its parameters for the user, one of which is completely arbitrary would be overridden in almost every scenario anyway. With the added overhead of making two unnecessary function calls -- one for the wrapper and one for trim() -- I can't imagine how a function like this could be any less useful.

  5. #5
    Fuck. SheenBR is offline
    ModeratorRank
    Feb 2008 Join Date
    Jaú, BrazilLocation
    2,433Posts

    Re: [PHP] Truncate String

    well, why dont you make one then?

  6. #6
    Account Upgraded | Title Enabled! AngraMainyu is offline
    MemberRank
    May 2011 Join Date
    445Posts

    Re: [PHP] Truncate String

    There's no need to wrap substr() to begin with.

  7. #7
    Fuck. SheenBR is offline
    ModeratorRank
    Feb 2008 Join Date
    Jaú, BrazilLocation
    2,433Posts

    Re: [PHP] Truncate String

    well, I have to agree with you in that.

  8. #8
    Web Developer Markshall is offline
    MemberRank
    Oct 2009 Join Date
    EnglandLocation
    628Posts

    Re: [PHP] Truncate String

    I like the idea of the $remove_spaces, that's a good idea.

    Made a little expansion on it and included SheenBRs idea:

    PHP Code:
    <?php 
    function truncate$str$allowed_length=10$remove_spaces=false$after_str="" 

        
    /*
            $str - the string to truncate
            $allowed_length - the amount of characters to limit to
            $remove_spaces - use trim() to remove white-space
            $after_str - another string to appear after the truncation, could be '...' if you like, so you could have a snippet of a story with "..." at the end
        */
        
    $str = ( $remove_spaces == true ) ? trim$str ) : $str;
        
        
    $return substr$str0$allowed_length );
        if ( !empty( 
    $after_str ) ) $return .= $after_str;
        
        return 
    $return;


    echo 
    truncate"Mark Eriksson is the best person ever!" ); //output: Mark Eriks 
    echo truncate"Mark Eriksson is the best person ever!"25 ); //output: Mark Eriksson is the best
    echo truncate"Mark Eriksson is the best person ever!"25false"..." ); //output: Mark Eriksson is the best...
    echo truncate"   Mark Eriksson is the best person ever!  "20true"..." ); //output: Mark Eriksson is the...
    ?>
    Last edited by Markshall; 01-12-11 at 01:33 PM.

  9. #9
    Banned Yamachi is offline
    BannedRank
    Oct 2006 Join Date
    Jolly EnglandLocation
    3,517Posts

    Re: [PHP] Truncate String

    As the others have pointed out, what is the purpose of writing a function that is simply a wrapper around a pre-existing function? That would be like me writing
    Code:
    def write(message) puts message end
    and then using
    Code:
    write "asd"
    instead of
    Code:
    puts "asd"
    Pointless... Just stick with using substr; That's what it was made for. Also, why would you set the default length to 10? Who would want a method that lets you grab only the first 10 characters of a string? I understand you're trying to get better with PHP and you're trying to help people, but this doesn't help anyone. I appreciate the gesture, though.

    BTW, you don't need to add the "== true" when using a boolean in a conditional. Simply using "if ($my_bool)" works fine and is cleaner.

  10. #10
    Web Developer Markshall is offline
    MemberRank
    Oct 2009 Join Date
    EnglandLocation
    628Posts

    Re: [PHP] Truncate String

    Quote Originally Posted by Yamachi View Post
    As the others have pointed out, what is the purpose of writing a function that is simply a wrapper around a pre-existing function? That would be like me writing
    Code:
    def write(message) puts message end
    and then using
    Code:
    write "asd"
    instead of
    Code:
    puts "asd"
    Pointless... Just stick with using substr; That's what it was made for. Also, why would you set the default length to 10? Who would want a method that lets you grab only the first 10 characters of a string? I understand you're trying to get better with PHP and you're trying to help people, but this doesn't help anyone. I appreciate the gesture, though.

    BTW, you don't need to add the "== true" when using a boolean in a conditional. Simply using "if ($my_bool)" works fine and is cleaner.
    I only used 10 characters as the default because the string I had used "Mark Eriksson blah blah" wasn't that long, so I chose 10 instead.

    I understand that 'if ( $my_bool )' works the same -- Lord knows what made me put == true lol



Advertisement