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

  • SEARCH
  • Home
  • 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 6154997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:21:33+00:00 2026-05-23T20:21:33+00:00

Looking for a bunch of things really. Essentially what I’m looking to create is

  • 0

Looking for a bunch of things really.

Essentially what I’m looking to create is the following string which can only contain MAX 140 chars (see where I’m going with this):

"Some text here from an XML feed... #tweeted http://bit.ly/123456" 

So I already have a $text variable which contains the text from the XML feed, this text string will always contain the string #tweeted and can be of any length.

Lets say thats 200 chars for this example, including the hashtag.

I know need to make room for the ...<space> and http://bit.ly/123456 (24 Chars in total)

  1. Remove the #hashtag E.g: $removehash = str_replace("#tweeted", " ", $text);

  2. Get the length E.g. $length = strlen($text);

  3. Trim it to 116, if over 140 chars.

  4. If under 116 chars do nothing because there is space for the … and the link

  5. add the … and the URL ($url).

Any advice, if you’ve done this previously, or any ideas on how to trim the string, remove the hashtag and apend to the end of the string to contrain it to a max of 140 chars greatly appreciated.

Not sure how to trim the text?

  • 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-23T20:21:34+00:00Added an answer on May 23, 2026 at 8:21 pm

    PHP’s substr function returns a portion of a string so you can, for example, get the first 116 chars of a string by

    $s = substr( $s, 0, 116 );
    

    It sounds like you are taking a 140 char string + #tweeted + URL and wanting to produce an output string of not greater than 140 chars? I would try something like:

    $s = "Some text here from an XML feed that is 140 chars or less #tweeted http://bit.ly/123456";
    $b = explode( "#tweeted", $s );
    
    $txt = trim( $b[0] );
    $url = trim( $b[1] );
    
    $txt = substr( $txt, 0, 139 - strlen( $url ) );
    
    $output = $txt . " " . $url;
    

    Obviously I’ve used too many temporary variables but hopefully that makes it clearer. Would also need to validate input string if necessary or the first explode will not return the expected result. The 139 in the sub is 140 char output – 1 char for the space in the result string.

    The substr line takes into account that the bitly URLs may be of different lengths and doesn’t just blindly cut off at 116 chars.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a place where I can download a bunch of CSS
Looking for C# class which wraps calls to do the following: read and write
I'm using EditorTemplates for things like DateTime, Colors, etc. My forms, which may contain
I'm looking to integrate a bunch of RSS/ATOM sources into a feed for our
I am looking for a way to have a Matrix variable (i.e, a bunch
I have a bunch of related indicies in a kind of templated hierarchy, looking
I've been looking around and I can't find a clear answer to my situation.
Someone has reported a bug in my program, which brings up the following error
I will keep this simple. I am looking everywhere to find something to create
I am still trying to create a full DOM document from a string. I

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.