How to show first 20 characters of a piece of text?

Hi,

I have a piece of text shown as follows:

How are you today, are you alright. Did you get your car fixed with the right parts.

Now how can I do it so that only the first 20 characters are displayed using php. Each character also includes spaces, puncuation, etc. Also the above is just an example, I will be working on dyanamic content.

Thanks!


$text = "How are you today, are you alright. Did you get your car fixed with the right parts.";

$pieces = substr($text, 0, 20);

echo $pieces;

Thanks very much.

I have learn’t how to do it in the past, but I had forget.

Thanks!

No problem. I do that too, if you ever forget, just refer to www.php.net, or as you’ve done, ask on SitePoint and there will always be someone who knows. :slight_smile:

Php.net and sitepoint are the two main resources I use when writing my php scripts.

I find sitepoint the best resource online for helping me with php when I am stuck.

Thanks for the help.

Glad to hear :slight_smile:

$pieces = preg_replace('/\\s\\S*$/', '', substr($text, 0, 21));

That should stop it breaking halfway along a word if you don’t like it doing so.