imulsion imulsion - 1 year ago 96
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.

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

will do the job.

Take a look here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download