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
You can do this by checking the length and using a substring when the description exceeds the length you set.
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.
And the foreach which processes its data as you required: