Search
 
SCRIPT & CODE EXAMPLE
 

PHP

length shorter

<?php
/**
*    Return an elipsis given a string and a number of words
*/
function elipsis ($text, $words = 30) {
    // Check if string has more than X words
    if (str_word_count($text) > $words) {

        // Extract first X words from string
        preg_match("/(?:[^s,.;?!]+(?:[s,.;?!]+|$)){0,$words}/", $text, $matches);
        $text = trim($matches[0]);

        // Let's check if it ends in a comma or a dot.
        if (substr($text, -1) == ',') {
            // If it's a comma, let's remove it and add a ellipsis
            $text = rtrim($text, ',');
            $text .= '...';
        } else if (substr($text, -1) == '.') {
            // If it's a dot, let's remove it and add a ellipsis (optional)
            $text = rtrim($text, '.');
            $text .= '...';
        } else {
            // Doesn't end in dot or comma, just adding ellipsis here
            $text .= '...';
        }
    }
    // Returns "ellipsed" text, or just the string, if it's less than X words wide.
    return $text;
}

$description = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quibusdam ut placeat consequuntur pariatur iure eum ducimus quasi perferendis, laborum obcaecati iusto ullam expedita excepturi debitis nisi deserunt fugiat velit assumenda. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Incidunt, blanditiis nostrum. Nostrum cumque non rerum ducimus voluptas officia tempore modi, nulla nisi illum, voluptates dolor sapiente ut iusto earum. Esse? Lorem ipsum dolor sit amet, consectetur adipisicing elit. A eligendi perspiciatis natus autem. Necessitatibus eligendi doloribus corporis quia, quas laboriosam. Beatae repellat dolor alias. Perferendis, distinctio, laudantium? Dolorum, veniam, amet!';

echo elipsis($description, 30);
?>
Comment

PREVIOUS NEXT
Code Example
Php :: insert three bars in php that are used to minimize and maximize pages 
Php :: Number in English Words (Indian format) php 
Php :: Undefined array key after unset() 
Php :: get first cat php wp 
Php :: laravel | eloquent | db | randomly fetch | query data 
Php :: wp php blog info background image 
Php :: laravel add model to one to many relationship 
Php :: laravel api get controller 
Php :: old codestar checkbox field 
Php :: laravel cors 
Php :: avoid data insertion if an error occurs in laravel 
Php :: php random string for filename 
Php :: index.php when deploying 
Php :: hirudhi 
Php :: Read the index and hashid of the last block in the blockchain 
Php :: "A non well formed numeric value encountered 
Php :: search bar php progress 
Php :: how to validate students who made payment in php and mysql 
Php :: Laravel Cache store [none] is not defined. 
Php :: php opencart controller 
Php :: joomla include jfactory 
Php :: 0 == "string" php 
Php :: remove public from url laravel 
Php :: Converting hiec to jpg using javascript before uploading in PHP 
Php :: drupal 9 custom access checking for routes 
Php :: code snippet for header footer in wordpress 
Php :: keep track of view count php 
Php :: error import php 
Php :: bsljeet 
Php :: dont allow this command to every one set in meddlware laravel 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =