0

I'm using the following to limit the amount of characters in a string

<?php $eventtitle1 = get_the_title();
$eventtitle1_str = strip_tags($eventtitle1, '');
echo substr($eventtitle1_str,0,30) . "…"; ?>

Is it possible to add the "..." if the string is more than 30 characters but not add it if it's less?

e.g

So it does this for a longer title:

"This is a longer ti..."

And it does this for a shorter title:

"This is a title"

(i.e not this - "This is a title ...")

Rob
  • 6,304
  • 24
  • 83
  • 189
  • 1
    I would recommend doing an `if statement` – RSM May 11 '12 at 08:39
  • Just check the length of the string before you shorten it. If it is more than 30 characters, trim and add the ellipsis. If not, do nothing. You need an `if` statement for that: http://php.net/manual/en/control-structures.if.php – Felix Kling May 11 '12 at 08:41
  • 1
    possible duplicate of [Shortening a string with ... on the end](http://stackoverflow.com/questions/4474517/shortening-a-string-with-on-the-end) and [possibly others](http://stackoverflow.com/search?q=php+truncate+string+ellipsis). – Felix Kling May 11 '12 at 08:44

10 Answers10

2
public function Truncate($string, $maxLen)
{
    if (strlen($string) > $maxLen)
    {
        return substr($string, 0, $maxLen) . '...';
    }
    return $string;
}
Lee Davis
  • 4,685
  • 3
  • 28
  • 39
2

Try this

<?php $eventtitle1 = get_the_title();
    $eventtitle1_str = strip_tags($eventtitle1, '');
    $strlen= strlen ( $eventtitle1_stng );
    if($strlen>30)
    echo substr($eventtitle1_str,0,30) . "…";
    else
    echo $eventtitle1_str;
     ?>
Rohit Choudhary
  • 2,253
  • 1
  • 23
  • 34
0

Try this:

if (strlen($eventtitle1_str) > 30) {
    $eventtitle1_str  = substr($eventtitle1_str,0,30) . "…";
}
Shomz
  • 37,421
  • 4
  • 57
  • 85
0
if ( strlen ( $eventtitle1_str ) > 30 ) {
  //Some logic
}
else {
  // Some logic
}
0

See strlen.

For example:

echo substr($eventtitle1_str,0,30) . (strlen($eventtitle1_str) > 30 ? "…" : "");
jimpic
  • 5,360
  • 2
  • 28
  • 37
0

You can use strlen for check number of characters.

<?php $eventtitle1 = get_the_title();
    $eventtitle1_str = strip_tags($eventtitle1, '');
     if(strlen($eventtitle1_str) > 30 ){     
       echo substr($eventtitle1_str,0,30) . "…"; 
    }else{
       echo substr($eventtitle1_str,0,30); 
     }

 ?>

thanks

Er. Anurag Jain
  • 1,780
  • 1
  • 11
  • 19
0
<?php
$eventtitle1 = get_the_title();
$eventtitle1_str = strip_tags($eventtitle1, '');

if (strlen($eventtitle1_str) > 30) {
    echo substr($eventtitle1_str, 0, 30)."…";
} else {
    echo $eventtitle1_str;
}
Zheng Kai
  • 3,473
  • 3
  • 17
  • 23
0

Besides the many right answers here I also recommend using the &hellip; entity in HTML instead of ... and also the MBSTRING extension.

So the code would look like:

$eventtitle1 = get_the_title();
$eventtitle1_str = strip_tags($eventtitle1, '');
if(mb_strlen($eventtitle1_str) > 30)
    echo mb_substr($eventtitle1_str, 0, 30) . "&hellip;";
} else {
    echo $eventtitle1_str;
}
shadyyx
  • 15,825
  • 6
  • 60
  • 95
  • @FelixKling Had it written as comment then decided to make it an answer... Is it this bad? Have no problem turning it into comment to the OP... – shadyyx May 11 '12 at 08:48
  • It's just not answering the question at hand, it's more like additional information, *in my opinion*. Others might think differently... do what you feel most comfortable with :) – Felix Kling May 11 '12 at 08:51
0

try this

echo substr_replace($eventtitle1_str, '...', 30);

check Example #1 here, hope this can help:
http://us.php.net/manual/en/function.substr-replace.php

adydy
  • 52
  • 2
0

I think its a Job for str_word_count http://php.net/manual/en/function.str-word-count.php

Example

$test = " I love to play foodtball";
var_dump ( substr ( $test, 0, 12 ) );
var_dump ( wordCount ( $test, 12 ) );

Output

string ' I love to p' (length=12)
string 'I love to play ...' (length=18)   

Can you see one is more readable than the other

Function Used

function wordCount($str, $max, $surffix = "...") {
    $total = 0;
    $words = str_word_count ( $str, 1 );
    $output = "";
    foreach ( $words as $word ) {
        $total += strlen ( $word );
        if ($max < $total)
            break;
        $output .= $word . " ";
    }
    $output .= $surffix ;
    return trim ( $output );
}
Baba
  • 94,024
  • 28
  • 166
  • 217