PHP Function to Limit String output or display

Posted by admin
This is a PHP Function thats limits string output or display. This is useful when you have an article and you want to show the first 200 characters of that article in order for you to show only full characters when users browse the whole or specific article.

While many function of this kind are already made, this function is a little bit different because it is cocern on displaying string that is W3C standards. To explain it, this function converts special characters to html entities and also remove tags on your string so that the script could count the characters accurately. Just read/run the code and you will see what I mean.

Please fell free to comment and improve this function. Thanks...!
function limit_chars( $string, $lenght=50, $allowable_tags='', $char_set='ISO-8859-1', $dots='...'){

if($allowable_tags != ''){
$char_set = strtoupper($char_set);

$string = strip_tags($string,$allowable_tags);
$string = html_entity_decode($string,ENT_QUOTES,$char_set);

$strlength = strlen($string);

if ( $strlength > $lenght ){
$limited = substr($string, 0, $lenght);
$limited .= $dots;
} else {
$limited = $string;
}

$limited = htmlentities($limited,ENT_QUOTES,$char_set);
} else {

$strlength = strlen($string);

if ( $strlength > $lenght ){
$limited = substr($string, 0, $lenght);
$limited .= $dots;
} else {
$limited = $string;
}
}

return $limited;
}
Back to List | Comments (2) | October 03, 2009 at 03:25 pm

2 Responses

mike says:
Jul 10, 2009 at 06:20 pm

ok, your code was nice dude....
have a nice day ma friend...

Beblie Tisoy says:
Aug 04, 2009 at 01:29 pm

This is very informative and useful... post especially for PHP developers, more post please.....

Leave a Reply

Name (required)

Email (required) will not be publish

Website (optional) ex: http://www.domain.com

Notify me of followup comments via e-mail. (optional)

Your Message (required)

Enter Security Code (required)
captcha  

Latest News (2)

iWebProvider.com No 1 at Yahoo search engine for the keyword -filipino web developer.

Posted on October 02, 2009

On Sept 6, 2009 iWebProvider.com is No. 1 at Yahoo and Google for keyword "filipino web developer".

continue reading

Launching of the updated iWebProvider.com

Posted on October 02, 2009

Now as of July 7, 2009 the launching of iWebProvider.com with its improve feature and the used of its very own CMS. It now has features like Blogs, comments on News articles and the upcoming user registration.

continue reading

Wise Words

"It's not about how many you have learned but how much you have utilized and mastered what you have learned."

- by: Mark Lester
Get subscribers