Use PHP to show a part of your text

Use PHP to show only a part of your text by adding “…” to the end of your message after 10 characters.



We’re gonna start with our text in a variable.

1
2
3
4
5
6
7
$message="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam in erat
nec lacus condimentum hendrerit. Vestibulum fringilla ornare augue ut sagittis.
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis
egestas. Vivamus urna nibh, tristique a suscipit sit amet, tempus vitae lectus.";

Okay here we go!!!
We use the PHP function strlen() to get the length of the text in our variable. Then use the PHP function substr() to only shoe the first 10 characters from the text in our variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
$VarLength = strlen($message);
//The length set to 10

if ($VarLength > 10) {
/*Add "..." after 10 charecters*/
$message = substr($message,0,10) . "...";
}
echo $message;

ADVERTISEMENT

Tags: , ,

Enjoyed this Post?

Subscribe to our RSS Feed, Follow us on Twitter or simply recommend us to friends!

Discussion

  • Frisian

    text-overflow: ellipsis now works with Firefox, too. Much simpler and also better looking.