what i am trying to do is basically shorten down a peice of text and add "..." to the end if the text is too long. However (here is where it gets hairy) i only want to apply this, if the last character of the string was a SPACE. If it is NOT space, then i want to go through the string (Backwords) until i find a space, THEN when a space is found i will add "..." to it.
The reasoning to this is i dont want to add "..." to a word when its not finished, such as "wor..." i would rather it be "word...".
Here is what i have tried so far (NOTE: $row_object_title is just a string coming from a database, so its safe to assume that its just a random text):
if (strlen($row_object_title) > 50){
// check for spaces
$short = substr($row_object_title,50);
$chr = substr($short,-1);
$ascii = ord($chr);
if ($ascii == "32"){
$row_object_title .= "...";
}else{
$x = 49;
while($ascii !== "32"){
$chr = substr($short,$x);
$ascii = ord($chr);
if ($ascii == "32"){
// we got a space!
$row_object_title .= "...";
}
$x = $x - 1;
}
}
}
Thanks for the help
UPDATE:
if (strlen($row_object_title) > 50){
// check for spaces
$short = substr($row_object_title,50);
$chr = substr($short,-1);
$ascii = ord($chr);
if ($ascii == "32"){
//$trim = "...";
//$final = str_replace($short,$trim,$row_object_title);
$row_object_title .= "...";
}else{
$x = 49;
while($ascii != "32"){
$chr = substr($short,$x,1);
$ascii = ord($chr);
if ($ascii == "32"){
// we got a space!
$row_object_title .= "...";
}
$x = $x - 1;
}
}
}
SECOND UPDATE: Every one is posting their version of the script (thanks alot) but could you guys possibly try to fix mine instead of posting other ways of doing it?