Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Crop data from database or Read more feature

So, for example, you have blog with news, and you want to do a preview page with all the news cut to 250 characters and Read more link at the end of each preview. To make it more difficult, you have this data with HTML code and unicode characters, so you can't cut in the middle of unicode char.

This function will help:

<?php
//$text - text to crop and $chars - amount fo characters
function crop_text($text, $chars)
{
    //checking if string needs to be cropped
    if(strlen(strip_tags($text)) > $chars)
    {
        //if yes we get out all HTML characters and cut string to needed amount of chars
        $text = substr(strip_tags($text), 0, $chars);
        //then to eliminate possibility to cut unicode in half 
        //we search last space in string to cut to full word
        $text = substr($text, 0, strrpos($text, ' '))."...";
    }
    else
    {
        //if no we just get out all HTML characters
        $text = strip_tags($text);
    }
    return $text;
}
?>

You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus