The obvious thing to do is read the documentation.
But to help: substr($str, $start, $end);
$str
is your text
$start
is the character index to begin at. In your case, it is likely 0 which means the very beginning.
$end
is where to truncate at. Suppose you wanted to end at 15 characters, for example. You would write it like this:
<?php
$text = "long text that should be truncated";
echo substr($text, 0, 15);
?>
and you would get this:
long text that
makes sense?
EDIT
The link you gave is a function to find the last white space after chopping text to a desired length so you don't cut off in the middle of a word. However, it is missing one important thing - the desired length to be passed to the function instead of always assuming you want it to be 25 characters. So here's the updated version:
function truncate($text, $chars = 25) {
if (strlen($text) <= $chars) {
return $text;
}
$text = $text." ";
$text = substr($text,0,$chars);
$text = substr($text,0,strrpos($text,' '));
$text = $text."...";
return $text;
}
So in your case you would paste this function into the functions.php file and call it like this in your page:
$post = the_post();
echo truncate($post, 100);
This will chop your post down to the last occurrence of a white space before or equal to 100 characters. Obviously you can pass any number instead of 100. Whatever you need.
substr()
? Where are you getting confused? It is one function that returns part of the source string, based on the parameters you give it.substr()
the first parameter is your text, the second is the offset - if set to 0 it will truncate from the beginning if set to 1,2,3... it will truncated after that many characters, the third argument is the length that should be truncated. For examplesubstr("hello world", 3, 4)
will returnlo w
- 4 characters after 3.