4

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!

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Craig van Tonder
  • 7,497
  • 18
  • 64
  • 109

5 Answers5

7

You can achieve this using substr.

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

fresskoma
  • 25,481
  • 10
  • 85
  • 128
Conner
  • 30,144
  • 8
  • 52
  • 73
  • Thanks for your reply, but could you please explain what is going on here? – Craig van Tonder Jul 09 '12 at 08:45
  • @BlackberryFan as it seems your too lazy to look in a manual [here is a link for you](http://php.net/manual/en/function.substr.php) – Manse Jul 09 '12 at 08:46
  • `substr(input, begin, end)` strips characters of a string, in this case it gets the characters from position 0 till position 197. Then it adds `'...'` to the string to finish it off. – Tgys Jul 09 '12 at 08:47
  • @ManseUK Thanks for your input... I have read many a manual, but find that an explanation from someone who isn't so scientifically minded helps me to understand the functions better. – Craig van Tonder Jul 09 '12 at 08:47
  • @BlackberryFan *Returns the portion of string specified by the start and length parameters.* is quite scientific .... – Manse Jul 09 '12 at 08:48
  • @Conner Actually your answer returns 201 characters. ;) – pvorb Jul 09 '12 at 08:49
  • Sorry, my mistake. -.- I should have had a look in the manual… :P – pvorb Jul 09 '12 at 08:51
  • @ManseUK I ask alot of question for my own reference... Its like my own manual of the things i've worked on... This is why I ask questions and will continue to do so... You should have posted an answer friend, Maybe you would have gotten it! – Craig van Tonder Jul 09 '12 at 08:51
  • @BlackberryFan There's a huge manual on php.net - learn to read it if you want to learn PHP. – Tom van der Woerdt Jul 09 '12 at 08:54
  • @TomvanderWoerdt As I said, its hard to find something you don't know the name of. Please read all of what I have to say before you start jumping to conclusions. But thank you for the input you have provided, it has provided me with understanding, which i appreciate. – Craig van Tonder Jul 09 '12 at 08:56
  • @BlackberryFan That's not what I meant. You say "*but find that an explanation from someone who isn't so scientifically minded helps me to understand the functions better*" so I suggest getting used to it. – Tom van der Woerdt Jul 09 '12 at 09:01
  • @TomvanderWoerdt Okay man thanks. I just meant that it helps me to understand things better when people who didn't make PHP explain it :) As far as this question goes, I now have two more approaches to think about, so it was more than worth it, if not now then in the future surely. – Craig van Tonder Jul 09 '12 at 09:05
  • @TomvanderWoerdt You do not learn things like what Cups has taught me on php.net, Just saying... – Craig van Tonder Jul 09 '12 at 10:19
6

This one takes care of strings that aren't actually 200 chars yet.

$string = 'your long string goes here';
if (strlen($string) >= 200) {
    $string = substr($string, 0, 197).'...';
}

Source: php.net/substr

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
5

May I give you an alternative made in pure CSS (good for headlines, for example)?

overflow: hidden; white-space: nowrap; text-overflow: ellipsis;

cuts off too long text automatically if the element around it is not long / big enough.

sascha
  • 4,671
  • 3
  • 36
  • 54
  • 1
    This is, in fact, a good idea. You could then add a `:hover` in the css that would show the full paragraph when you hover the mouse over it. – Tom van der Woerdt Jul 09 '12 at 08:48
  • Please, don't forget, this is not working in "older" browsers! – sascha Jul 09 '12 at 08:50
  • @Sn0opy Thanks a lot, I did think about this but I was not sure of how browser friendly this would be. Do you have any info relating to this? – Craig van Tonder Jul 09 '12 at 08:54
  • @BlackberryFan I recommend checking http://w3schools.com by yourself. They've got a huge list of every CSS property and its values including compatibility and issues – sascha Jul 09 '12 at 08:58
  • @Sn0opy Thanks I will do that, its always nice to hear someone else's opinion though! – Craig van Tonder Jul 09 '12 at 09:02
4
$string = 'asd';
$maxLength = 200;
if (mb_strlen($string) > $maxLength) {
    $string = mb_substr($string, 0, $maxLength) . "...";
}
Sergey Eremin
  • 10,994
  • 2
  • 38
  • 44
2

Only collect 200 characters from your database in the first place.

$sql = "SELECT LEFT(article, 200) as article WHERE ETC ETC "

Then apply something like this, but essentially you need to go back to the previous word end chop off the rest and apend an elipsis.

…
Cups
  • 6,901
  • 3
  • 26
  • 30
  • This is an interesting approach :) Thanks! – Craig van Tonder Jul 09 '12 at 08:54
  • "elipsis" I never knew that one, its awesome thanks... The link provided seems to be a really good solution for this too! – Craig van Tonder Jul 09 '12 at 09:09
  • Selecting less data would more than likely be the better idea too! – Craig van Tonder Jul 09 '12 at 09:13
  • Thanks for your input again, this has proved to be the most useful solution. Selecting only a minimum amount of data, then running it through the function as described in the link creates a really useful process. This is as the string comes out trimmed and neat. I have made the addition to the function of adding multibyte support as described by @kgb in his answer. – Craig van Tonder Jul 09 '12 at 10:06