kj007
kj007

Reputation: 6254

How to strip text in table column according to column width in PHP

What I want actually, I have a title which length is not fixed so when I would like to show string text in table for title column, I would like to strip text with ... if text is bigger than column width..

so here is example:

  1. I have table where column name is "Title" and when I am showing Title Name="This is KC, THis is KC, This is KC" in tiltle column and column width is lets say 10% of table then it should show "This is KC, This is..." .

Please advise.

Upvotes: 1

Views: 90

Answers (3)

user2663434
user2663434

Reputation:

i dont know about ur column width but u can add limit for Title

$string = "sdsdddsddds";
$string = strip_tags($string);

if (strlen($string) > 5) //u can add ur limit of character here
{
    $stringCut = substr($string, 0, 5); //u can add ur limit of character here

    $string = substr($stringCut, 0, strrpos($stringCut, ' ')).'... <a href="/this/story">Read More</a>'; 
}
echo $stringCut."".$string;

Upvotes: 0

Duikboot
Duikboot

Reputation: 1101

You can substr your result so it will only show a part of the result. I made an example to show you how.

<?php
$yourstring = 'This is KC, THis is KC, This is KC';
$rest = substr($yourstring, 0, 15);  
echo $rest;

Upvotes: 0

feeela
feeela

Reputation: 29932

You can't achieve such an effect with PHP, since you don't know how wide your table is depending on different client screen widths. You could do so if you know the font, the font size and the column width in pixel and even then it is still a lot of arithmetics.

But there is a CSS property that does what you want: text-overflow: ellipsis;

Using this property the content is clipped and optionally appended by an ellipsis (…) if the box is to narrow.

See: https://developer.mozilla.org/en-US/docs/Web/CSS/text-overflow (browser support at bottom)

Upvotes: 2

Related Questions