I was wondering how you would show only the first "x" characters of a post, as a preview. Sort of like what StackOverflow does when they show there list of questions.
The quick brown fox jumps over the lazy dog
goes to
The quick brown fox jumps...
I dont want to split up a word in the middle. I was thinking the explode function splitting up at every space , explode(" ", $post), but I wasnt too sure if there was another way or not. Thanks
Try:
preg_match('/^.{0,30}(?:.*?)\b/iu', $text, $matches);
which will match at most 30 chars, then break at the next nearest word-break
strpos ( http://www.php.net/strpos ) will give you what you need. This function should give you what you need.
function getPreview($text, $minimumLength=60){
return substr($text,0,strpos($text,' ',$minimumLength)) . '...';
}
Note: I haven't tested that function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With