views:

1205

answers:

5

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

A: 
    $cutoff = 25;
    if ($i < $cutoff)
    {
        echo $str;
    }
    else
    {
        // look for a space
        $lastSpace = strrchr(substr($str,0,$cutoff)," ");
        echo substr($str,0,$lastspace);
        echo "...";
    }
Byron Whitlock
split(" ", strlen($str)); looks wrong.
Lior Cohen
yeah, i removed it thanks.
Byron Whitlock
+4  A: 

If you only want to cut the string, without doing it in the middle of a word, you might consider using the wordwrap function.

It will return a string with lines separated by a newline ; so, you then have to explode that string using \n as a separator, and take the first element of the returned array.


For more informations and/or examples and/or other solutions, see, for instance :

Pascal MARTIN
Hmm.. have to say, this is a really smart thinking. thanks for the tip. I applied it..it works fast and it costs one line of code. regards
Ahmad Fouad
+1 neat. never used the wordwrap function before.
Byron Whitlock
@Ahmad : you're welcome :-) ; @Byron : one of the nice things with PHP is that there's always something left to discover ;-) (and that's a reason why I like SO ^^ )
Pascal MARTIN
A: 

This will cut off at either 60 characters or the first space after 60 characters, identical to your initial code but far more efficient:

$position = 60;
if(substr($var,$position,1) == " ") $position = strpos($var," ",$position);

if($position == FALSE) $result = $var;
else $result = substr($var,0,$position);
Amber
A: 
$matches = array();
preg_match('/(^.{60,}?) /', $text, $matches);
print_r($matches[1]);

Then you have to add the ellipses if needed.

phantombrain
A: 
<?php

// same as phantombrain's but in a function
function uc_textcut($text) {
    $matches = array();
    preg_match('/(^.{60,}?) /', $text, $matches);
    if (isset($matches[1])) {
        echo $matches[1] . "...";
    } else {
        echo $text;
    }
}


// test it
$textLong = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus dui non sapien ullamcorper vel tincidunt nisi cursus. Vestibulum ultrices pharetra justo id varius.';
$textShort = 'Lorem ipsum dolor sit amet.';

uc_textcut($textLong);
echo "\n";
uc_textcut($textShort);

?>

Prints:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed...
Lorem ipsum dolor sit amet.
Lance Rushing