Hello,
I am trying to limit the number of characters returned from a string using PHP. I've applied a solution that just seemed to crash the server (high load) / infinite loop. So I am asking for alternative,
Simply, I am trying to find a solution that cuts the string, display specific amount of characters, but still respect the meaning of the sentence. (e.g. Does not cut in the middle of the word)
My function call is:
<?php
uc_textcut(get_the_title());
?>
And in my functions.php this is the code i used and it does crash.
function uc_textcut($var) {
$position = 60;
$result = substr($var,$position,1);
if ($result !=" ") {
while($result !=" ") {
$i = 1;
$position = $position+$i;
$result = substr($var,$position,1);
}
}
$result = substr($var,0,$position);
echo $result;
echo "...";
}
My problem is with $position = 60. The higher, the more load it takes..like its doing a very slow loop. I imagine something is wrong with while(). But I am trying to make it still understandable by the visitor.. not cutting in the middle of major word.
Any input?
:) thanks very much guys