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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:03:24+00:00 2026-06-09T18:03:24+00:00

I am using a custom function to truncate text based on an argument which

  • 0

I am using a custom function to truncate text based on an argument which states the number of allowed characters. Here is my code:

function bm_best_excerpt($length, $ellipsis, $content) {
$text = $content;
$text = strip_tags($text);
$text = substr($text, 0, $length);
$text = substr($text, 0, strripos($text, " "));
$text = $text.$ellipsis;
return $text;
}

ON any page, I can truncate text by grabbing a WordPress custom field (which contains a string of text) and running it through this function like so:

<?php $excerpt = get_field('long_description'); ?>
<p><?php echo bm_best_excerpt(70, ' ... ', $excerpt); ?></p>

This works great, and displays the first 70 characters of $excerpt. I want to modify this function, so it first counts the number of characters in a title (above the description) and then uses any leftover characters for this description.

To illustrate this, lets say I want to allow for 80 characters total. My RAW HTML is below:

Here is a Title that is fairly long

Here is a description followed by some Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

I want the TOTAL amount of characters between the title and description to be 80. The title has 35 characters, which leaves 45 characters left for the description.

How would I modify the above function to allow for the counting of an additional content variable (as an argument) and then the subtraction against the $length? I would want the function to look something like this:

function bm_best_excerpt($length, $ellipsis, $content1, $content2) {

Where $content1 is the title and $content2 is the description

  • 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-06-09T18:03:26+00:00Added an answer on June 9, 2026 at 6:03 pm

    Try this to add the strings together and process as one:

    function bm_best_excerpt($length, $ellipsis, $content, $content2='') {
        // default content2 to empty string, allowing use of old method to still be valid
        $text = $content.' '.$content2; // add the second content to the end of the first
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a custom random number function rand48 in CUDA. The function does not
I am getting strange behaviour using the built-in C# List.Sort function with a custom
I have a custom binding I am using to truncate a description in an
I am using a custom function to populate the OG info into the head
I want to add a custom function using jquery validation framework, to help validating
I wrote a small program using a custom indexOf function but wanted to dismiss
I´m using custom cells in a TableView. The cell height is calculated based on
Am using Custom Google search in my web page Code: <div id=cse style=width: 100%;>Loading</div>
I've been using a custom function for some time now to prevent clickjacking on
I'm using a custom taxonomy and a custom post type: <?php function my_custom_taxonomies() {

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.