String Cut / Read More

Posted by Pete | Posted in PHP Functions | Posted on 17-07-2009-05-2008

1

Youve seen it done on new content systems. Now we will show you how you too can show only part of a string weather this is to fit a template box or just to make your readers investigate your site more.

function strcut($string, $length=25){ // Start Function with string and length fields with length default to 25 characters.
    $strl = strlen($string); // Get current string length.
    $strl = $length - $strl; // take string length away from length of characters you want.
    $sub = substr($string, "0", $strl). "..."; // Actually cut the string and add ... to the end of the string.
    return $sub; // Return the string
}

Below is a demo on how this can be used.

function strcut($string, $length=25){
    $strl = strlen($string);
    $strl = $length - $strl;
    $sub = substr($string, "0", $strl). "...";
    return $sub;
}
$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer sem diam, cursus eu convallis nec, placerat eget neque. Nunc est ante, ultrices eget pellentesque ut.";
if(isset($_GET[full])){
    echo "". $string. "Read Less";
}else{
    echo "". strcut($string, "15"). "Read More";
}

For any further help or if you like this function please leave a comment.

Comments posted (1)

This is a sick idea :P .

Write a comment

You must be logged in to post a comment.

Tutorials Written By Peter Kelly