Home > PHP > Php String Truncate

Php String Truncate

<?php

function myTruncate2($string, $limit, $break=" ", $pad="…") {
// return with no change if string is shorter than $limit
if(strlen($string) <= $limit) return $string;
$string = substr($string, 0, $limit);
if(false !== ($breakpoint = strrpos($string, $break))) {
$string = substr($string, 0, $breakpoint); }
return $string . $pad;
}

$description = 'Enter title enter title enter title enter title enter title enter title enter title tenter title';

$shortdesc = myTruncate2($description, 50); echo "

$shortdesc

“;

?>

Advertisements
Categories: PHP
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: