skip to Main Content

Let’s say I have a php loop on output

<?php foreach ($items as $item) { ?>
  <div class="title"><?= $item->title ?></div>
  <div class="description"><?= $item->description?></div>
<?php } ?>

As a result, in html I get the following

<div class="title">What is Lorem Ipsum?</div>
<div class="description">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>

Now my description field has 566 characters

How can I make the maximum number of characters for the description field on the php side so that the maximum number of characters for the description field is, say, 230 and it is displayed like this with three dots at the end?

<div class="title">What is Lorem Ipsum?</div>
<div class="description">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type...</div>

2

Answers


  1. You can do this by checking the length and using a substring when the description exceeds the length you set.

    <div class="description">
        <?php echo (strlen($item->description) > 230) ? substr($item->description, 0, 230) . '...' : $item->description ?>
    </div>
    
    Login or Signup to reply.
  2. You can use the PHP substr function which allows you to extract a portion of a string based on a starting index and a length.

    Below i created an example file with sample data.

    <?php
        //Example items
        $items = [
            (object) [
                'title' => 'Long text',
                'description' => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.'
            ],
            (object) [
                'title' => 'Short text',
                'description' => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.'
            ]
        ];
    ?>
    

    And the foreach which processes its data as you required:

    <?php foreach ($items as $item) : ?>
        <div>
            <?= $item->title; ?>
        </div>
        <div>
            <!--Check if description length is greater than 230 characters-->
            <?php if(strlen($item->description) > 230): ?>
                <!--If yes, truncate the description and append "..." at the end-->
                <?= substr($item->description, 0, 230) . "..."; ?>
            <?php else: ?>
                <!--If not, display the full description-->
                <?= $item->description; ?>
            <?php endif; ?>
        </div>
    <?php endforeach;?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search