Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 890757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:47:03+00:00 2026-05-15T13:47:03+00:00

How can I get the first n characters of a string in PHP? What’s

  • 0

How can I get the first n characters of a string in PHP? What’s the fastest way to trim a string to a specific number of characters, and append ‘…’ if needed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:47:04+00:00Added an answer on May 15, 2026 at 1:47 pm
    //The simple version for 10 Characters from the beginning of the string
    $string = substr($string,0,10).'...';
    

    Update:

    Based on suggestion for checking length (and also ensuring similar lengths on trimmed and untrimmed strings):

    $string = (strlen($string) > 13) ? substr($string,0,10).'...' : $string;
    

    So you will get a string of max 13 characters; either 13 (or less) normal characters or 10 characters followed by ‘…’

    Update 2:

    Or as function:

    function truncate($string, $length, $dots = "...") {
        return (strlen($string) > $length) ? substr($string, 0, $length - strlen($dots)) . $dots : $string;
    }
    

    Update 3:

    It’s been a while since I wrote this answer and I don’t actually use this code any more. I prefer this function which prevents breaking the string in the middle of a word using the wordwrap function:

    function truncate($string,$length=100,$append="…") {
      $string = trim($string);
    
      if(strlen($string) > $length) {
        $string = wordwrap($string, $length);
        $string = explode("\n", $string, 2);
        $string = $string[0] . $append;
      }
    
      return $string;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I get the first character of a string that works both in
Question Using XSLT 1.0, given a string with arbitrary characters how can I get
I have an MD5 alpha-numeric. How can I get the first five characters and
How can I get first and last days of the current week? What I
I remember seen some code xaml where can get the first element (like an
I can get the the first row in a ListView item in .NET 3.5
How can I ignore first element and get rest of the elements? <ul> <li><a
Can get all triples with value null in specific field? All people with date_of_birth
I am looking for a way to pull the first 100 characters from a
Possible Duplicate: How to get first x chars from a string, without cutting off

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.