Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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
Advertisements
