- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Shorten string with a “…” body in PHP?
Let’s say the following is our string −
$sentence="This is my first PHP program";
We want the following output −
This is my first PHP ... gram
We shorten the string with “…”. For this, use the concept of substring() and set in a condition by checking the number of words −
Example
<!DOCTYPE html> <html> <body> <?php $sentence="This is my first PHP program"; if (strlen($sentence) >= 13) { echo substr($sentence, 0, 20). " ... " . substr($sentence, -4); } else { echo $sentence; } ?> </body> </html>
Output
This is my first PHP ... gram
- Related Articles
- Check if a string starts with given word in PHP
- Check if a string ends with given word in PHP
- PHP preg_split to split a string with specific values?
- How do you make a string in PHP with a backslash in it?
- PHP to check substring in a string
- Remove comma from a string in PHP?
- What happens if ++ operator is used with string value in PHP?
- PHP – Case folding in a string using mb_convert_case()
- PHP fetch a specific value that begins with a given number from a string with letters and numbers?
- PHP String Operators
- Remove new lines from a string and replace with one empty space PHP?
- PHP $string{0} vs. $string[0];
- How to convert a string into number in PHP?
- Detect language from string in PHP
- PHP String Data Type

Advertisements