I want to limit the length of text shown in php. What is the code in php? But i want to keep the full of text in database.

Member Avatar for cuonic

Hi cliffc,

Say you have a string called $fulltext, that is the full text string and you only want to display up to 20 chars of it to the user at the current time, then use the following code :

<?php

$fulltext = "This is the LM pilot. I'd like to take this opportunity to ask every person listening in, whoever and wherever they may be, to pause for a moment and contemplate the events of the past few hours and to give thanks in his or her own way."; // Full string to shorten

$shorttext = substr($fulltext), 0, 20); // Result : "This is the LM pilot"

echo $shorttext;

?>
commented: useful +4
Member Avatar for diafol

If you find non-ASCII acharacters in your text, you'll need to use:

$shorttext = mb_substr($fulltext, 0, 20, 'UTF-8');
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.