imulsion imulsion - 9 months ago 51
PHP Question

Shorten a text string in PHP

Is there a way to trim a text string in PHP so it has a certain number of characters? For instance, if I had the string:

$string = "this is a string";

How could I trim it to say:

$newstring = "this is";

This is what I have so far, using
, but it isn't working. Can anyone improve on my method?

function trimtext($text)
$newtext = chunk_split($text,15);
return $newtext;

I also looked at this question, but I don't really understand it.

if (strlen($yourString) > 15) // if you want...
    $maxLength = 14;
    $yourString = substr($yourString, 0, $maxLength);

will do the job.

Take a look here.