I am using this to shorten long titles: <?php $title = $row['adtitle']; if (strlen($title) > 50) { $title = substr($title, 0, 50).'...'; } echo $title; ?> Code (markup): The thing is it shortens them this way: Selling housing in OH. Stop by to check it ou... Make money working from home. Best opport... I want it to shorten them right before the last word, so that it would look like this: Selling housing in OH. Stop by to check it... Make money working from home. Best... Any ideas how to do that? Thanks!
Try this: $title = $row['adtitle']; if (strlen($title) > 50) { $title = preg_replace('~\s+\S+$~', '', $title); $title = $title."..."; } echo $title; PHP:
Somebody on another forum pointed out that one line was missing (which probably you just forgot to add): <?php $title = $row['adtitle']; if (strlen($title) > 50) { $title = substr($title, 0,50); $title = preg_replace('~\s+\S+$~', '', $title); $title = $title."..."; } echo $title; ?> Code (markup): Nonetheless, your code is working well too.