Question

I am trying to limit the number of characters returned from a string using PHP.

I've applied a solution that seems to crash the server (high load / infinite loop), so I am asking for alternative.

I am trying to find a solution that cuts the string and displays the specific number of characters, but still respects the meaning of the sentence, i.e. it does not make a cut in the middle of the word

My function call is as follows:

<?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 that number, the more load it takes -- like its doing a very slow loop.

I imagine something is wrong with while(), but I am trying to keep it still understandable by the visitor, again, not cutting in the middle of word.

Any input?

:) thanks very much guys

Was it helpful?

Solution

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 :

OTHER TIPS

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);
    $cutoff = 25;
    if ($i < $cutoff)
    {
        echo $str;
    }
    else
    {
        // look for a space
        $lastSpace = strrchr(substr($str,0,$cutoff)," ");
        echo substr($str,0,$lastspace);
        echo "...";
    }
$matches = array();
preg_match('/(^.{60,}?) /', $text, $matches);
print_r($matches[1]);

Then you have to add the ellipses if needed.

<?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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top