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 6705451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:24:20+00:00 2026-05-26T07:24:20+00:00

I use a truncate method to truncate a string in my site. I truncate

  • 0

I use a truncate method to truncate a string in my site. I truncate with the code:

 truncate(auto_link(textilize(post.content)), :length  => 140)

This truncation truncates the post content, but there is a problem with the content contains links. EG the post content is:

 <p>Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo 
 <a href="http://www.yahoo.com">Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo 
 Yahoo </a> Google Google Google Google Google Google Google Google Google Google  <a 
 href="http://www.google.com"> Google Google Google Google Google Google Google Google 
 </a><br></p>

In this case, the all the html tags and the links get counted in the character count by truncate method, rather than the truncate method counting the characters that a viewer would actually see:

 Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo
 Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo  (etc..)

Is there any work around here for the truncation method?

EDIT: new example to clear up ambiguities:

Users post their posts in plain text, and are allow to use textile for some formatting. In terms of posting links, I allow users to (a) paste plain urls (http://www.site.com, http://www.site.com, etc..), (b) use textile syntax which converts “the click me text”:http://www.example.com to the click me text or take their time to embed links in html form.

I want a page that has snippets of a person’s posts to show only the first, say, 140 characters of their post. But I want this to be 140 characters of the text that a reader would see. In other words, let’s say a user’s post starts as:

 Today I went to the market to pick up some fruit. "At the market there was this awesome
 fruit display!":http://www.externalsite.com/picture.jpg Definitely want to go back 
 tomorrow!

This will be converted by textilize to a string of 193 characters:

 Today I went to the market to pick up some fruit. <a href="http://www.externalsite.com/picture.jpg"> 
 At the market there was this awesome fruit display!</a> Definitely want to go back 
 tomorrow!

The text the user will see on a page is only 138 characters:

 Today I went to the market to pick up some fruit. At the market there was this awesome
 fruit display! Definitely want to go back tomorrow!

The 193 character long text would be truncated by truncate, but if I had a work around the whole text would display, ending up appearing as just 138 characters with the link embedded.

  • 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-26T07:24:20+00:00Added an answer on May 26, 2026 at 7:24 am

    For those interested, to get an accurate count without links, one can do:

     count = strip_tags(string).count
    

    (This is for a string that has html tags in it. If the string needs to be ‘textilized’ etc.. first, then the code is count = strip_tags(textilize(string)).count).

    Rather than use truncate, I just limited the count to 140 characters of this true count ie switched this to a validation on the field.

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

Sidebar

Related Questions

A senior member here gave me this code: public static string Truncate(this string value,
I am trying to use this method to draw a string into a custom
I have this block of code that eventually get serialized to JSON, for use
I use this little function to truncate strings when needed: function truncate_text($text, $nbrChar =
Currently, the method I use to truncate strings is: echo substr($message, 0, 30)...; How
I made a function to truncate a string in the code behind file. But
I am trying to use the truncate filter on 2 variables (rsstitle and rssdescription),
use this website a lot but first time posting. My program creates a number
Does anyone know how to truncate a string in a JSP using a tag
I have two views which will both use the same Controller method: //webServiceController.cs //The

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.