0

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

5 Answers 5

4

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 :

Sign up to request clarification or add additional context in comments.

2 Comments

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 : 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 ^^ )
0

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);

Comments

0
    $cutoff = 25;
    if ($i < $cutoff)
    {
        echo $str;
    }
    else
    {
        // look for a space
        $lastSpace = strrchr(substr($str,0,$cutoff)," ");
        echo substr($str,0,$lastspace);
        echo "...";
    }

Comments

0
$matches = array();
preg_match('/(^.{60,}?) /', $text, $matches);
print_r($matches[1]);

Then you have to add the ellipses if needed.

Comments

0
<?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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.