📲 Download Our Apps

EMI Calculator

Download EMI Calculator

SOA Games

Download SOA Games

SOA Technology App

Download SOA Technology

BMI Checker

Download BMI Checker

Task Jira

Download Task Jira

Laughing Adda

Download Laughing Adda

📅 हिंदी कैलेंडर ऐप डाउनलोड करें

Download Shubhcalendar App

truncate character in php if exceed length

To truncate a string in PHP if its length exceeds a specified limit, the substr() function is commonly used.

Here is an example of how to truncate a string and append an ellipsis if it exceeds a certain length:

<?php

function truncateString(string $text, int $maxLength, string $ellipsis = '...'): string
{
    if (strlen($text) > $maxLength) {
        return substr($text, 0, $maxLength) . $ellipsis;
    }
    return $text;
}

$longString = "This is a very long string that needs to be truncated if it exceeds a certain length.";
$shortString = "Short text.";

echo "Truncated long string: " . truncateString($longString, 20) . "\n";
echo "Short string (not truncated): " . truncateString($shortString, 20) . "\n";

?>

In this example:

  • truncateString function: This function takes three arguments:
    • $text: The original string to be truncated.
    • $maxLength: The maximum desired length of the string before truncation.
    • $ellipsis: (Optional) The characters to append to the truncated string (defaults to ...).
  • strlen($text): This function calculates the length of the string.
  • Conditional Check: The if statement checks if the length of $text is greater than $maxLength.
  • substr($text, 0, $maxLength): If the string is too long, substr() extracts a portion of the string starting from the beginning (offset 0) up to the specified $maxLength.
  • Concatenation: The extracted substring is then concatenated with the $ellipsis to indicate truncation.
  • Return Original String: If the string is not longer than $maxLength, the original string is returned unchanged.



Leave a Reply

AI Spiritual Tools & Interactive Experiences

Explore powerful AI-driven tools for daily guidance, spirituality, fun quizzes, and self-discovery.

Today’s Quote

Get inspiring daily quotes powered by AI to motivate and guide your day.

Explore Now

AI Tarot Card Reader

Reveal insights about your future, love, and career with AI tarot readings.

Read Tarot

Love Match Calculator

Check compatibility and love predictions using AI-based analysis.

Check Match

Fortune Cookie

Open an AI fortune cookie and receive wisdom, luck, and fun messages.

Open Cookie

Quiz Categories

Engage with knowledge-based and fun quizzes across multiple categories.

Start Quiz

Panchang Calendar

View daily Panchang, auspicious timings, tithi, nakshatra, and festivals.

View Panchang

Online Numerology

Discover your destiny number, life path, and numerology predictions.

Calculate Now

Spiritual Feeds

Stay connected with spiritual thoughts, mantras, and divine content.

View Feeds

Quiz Hub

Attempt trending quizzes on GK, spirituality, festivals, and more.

Explore Quizzes