I'm going to assume what you mean is that at the nth character, if it is not a space, continue up to a space, cut it off there and add the 3 dots?
$str = 'This is a sample string that needs to be sampled!';
preg_match('#^(.{13}[^ ]*)#', $str, $match); // assume nth spot is 13 characters long...
echo $match[1] . '...';
Output:
This is a sample...
In this case, the 13th character is the m in the word sample.. so the regex pattern will continue to capture anything that is not a space (zero or more times).. so 'ple' is also included with the capture to complete the word sample, then add the 3 dots... Is that what you are looking for?
EDIT - Granted, if the nth character lands on a space, my preg solution will match all the way up to the next space.. perhaps this alternative is more accurate?
$str = 'This is a sample string that needs to be sampled!';
$cutOff = 13; // set this as the nth value
if(substr($str, ($cutOff-1), 1) == ' '){ // nth position lands on a space
$result = substr($str, 0, $cutOff);
} else { // nth position does not land on a space
$findSpace = strpos(substr($str, ($cutOff-1)), ' ');
$result = substr($str, 0, ($cutOff + $findSpace));
}
echo $result .= '...';