A simple Cut function

Discussion in 'PHP' started by Scripten, Feb 5, 2009.

  1. #1
    Yo everybody, here is a PHP Script with a function named "Cut" which can cut a "cut" a string for example:

    
    <?php
    include("./cut.php");
    echo Cut("Tobi is a good boy!", 12);
    ?>
    
    PHP:
    The result from this script will be "Tobi is a go..." :)

    Well here's the script:

    
    <?php
    function Cut($text,$len) {
    $string = $text;
    $string_num = strlen($string);
    if($string_num > $len) {
     $string = substr($string,0,$len);
     echo $string . "...";
     } else {
      echo $string;
     }
    return $string;
    }
    /*
    Function created by Scripten
    Site/Portfolio: None for now :)
    E-mail: scriptenx@yahoo.com
    */
    ?>
    
    PHP:
    Hope u like it :)
     
    Scripten, Feb 5, 2009 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    function cut( $text, $length = 6 )
    {
        return strlen( $text ) > $length ? substr( $text, 0, $length ) . '...' : $text;
    }
    PHP:
     
    Danltn, Feb 5, 2009 IP
  3. Scripten

    Scripten Peon

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Naaa...I like the complicated codes :)
     
    Scripten, Feb 15, 2009 IP