ord()
returns an integer, but you check the value of $ascii
using !==
and compare it with a string. As opposed to !=
, !==
is only true if the types of the operands match too.
In addition, to extract a single character from the string, specify a length for the substring:
$chr = substr($short,$x,1);
You may also index the string directly: $chr = $short{$x}
Edit: Found two flaws more:
The second line should be:
$short = substr($row_object_title, 0, 50);
(You've missed the offset before the length parameter.)
Furthermore you're appending '...'
to the original string, not to the shortened version. Below is a complete version of the (hopefully) corrected script.
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 = $short . "...";
}else{
$x = 49;
while($ascii != 32){
$chr = substr($short,$x,1);
$ascii = ord($chr);
if ($ascii == 32){
// we got a space!
$row_object_title = substr($short, 0, $x) . "...";
}
$x = $x - 1;
}
}
}