Portion() function for trimming text

Returns the portion of text specified by the start and length parameters. This help to trim text down for display in a small area or to give a sample of a larger body of text.

I created this function to display a portion of listings on my home page under their related links to the full body of content.

This can helps your visitor to find the content faster as well as helps search engine better index your web site.

Portion() Code:

// function starts here
function portion($max_len,$str){
$max_len = 12; // default max length of characters including blank spaces
if (strlen($str) > $max_len){
echo substr("$str", 0, $max_len)."..."; // your portion of text with trailing dots "..."
echo $str;
// function code end here
$str = "Hello World"; // edit or delete this line for your use
portion(7,"$str"); // call the function ( this will return "Hello W..." )

You may also like...

Leave a Reply