Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - How to append "..." to a string of varying length at a certain point? e.g. After 200 chars

Tags:

string

php

i have a mysql database and a table full of information, specifically a column of product descriptions. This column is used in my scripts twice. Its max length is 1000 characters.

One of the uses for this information is a brief description of any given product. I would like to display a shortened version of the description that I have just mentioned, so it will be say for instance 200 characters long.

Now, using PHP I would like to append the string with "..." (3 dots) at the point where it reaches 200 characters. So basically only the first 200 characters are displayed.

How would I go about something like this? I am sure it will be a fairly simple task but the internet is a big place to look for something you don't know the name of :)

If anyone could help in this regard, with an example or some links relating to the relevant functions that would be great. Thanks a lot!

like image 373
Craig van Tonder Avatar asked Nov 28 '22 16:11

Craig van Tonder


1 Answers

You can achieve this using substr.

$string = substr($string,0,197) . "...";

like image 136
Conner Avatar answered Apr 13 '23 00:04

Conner