Hi,
I have a news page which selects the first 75 charecters of text from the main news text.
What i want to do is clean it up.
Sometimes the text prints out half words, so I want to strip everything after the last space in the text so that the last word is a full one.
Any ideas?
Thanks in advance!
I have many ideas. Most are not fit for a public forum...
Use strrpos()
$msg = 'Sometimes the text prints out half words, so I want to strip everything after the last space in the text so that the last word is a full one.'; $seventyfive = substr($msg, 0, 75); $pos = strrpos($seventyfive, ' '); $short_msg = substr($seventyfive, 0, $pos); echo $short_msg;
I'd use preg_replace, but that's just personal preference. Simply search for the pattern "whitespace, word, end of string," and replace it with ellipses:
$msg = preg_replace("§\s\w$§i";"...";$msg);