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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:21:10+00:00 2026-06-13T10:21:10+00:00

So I’m running into what is probably a pretty common problem when creating dynamic

  • 0

So I’m running into what is probably a pretty common problem when creating dynamic websites that pull page information from a database. Say you have a slider on the front page that shows the 8 most recent blog/story posts. The server grabs the title, caption, and text fields for each, but we don’t want to return the full text of the article to the browser, that would be wasteful. So what I’m guessing you would do is cut the text field off at a certain amount of characters using a recursive function, except I can’t seem to figure out what I’m doing wrong. Here’s the code:

$string = strip_tags("<p>Jackson expects to practice on Wednesday for the first time since getting hurt in a season-opening game agains the Chicago Bears. The teams medical advisor says his sprained ankle has healed fast then expected, although he warns to err on the side of caution.</p><p>Coach Andy Reid is optimistic he can get Jackson ready in time for next Monday's square off vs the Detroit Lions, though he states that he doesn't want to take the risk of loosing him for the start of the playoffs in 3 weeks.</p>");
$count = 0;

echo $string."<br />";

function trim_string($string, $max_length, $search_char){
    global $count;
    $pos = strripos($string, $search_char);
    $new_string = substr($string, 0, $pos);
    $length = strlen($new_string);  

    if($length > $max_length){      
        $count++;
        trim_string($new_string, 120, ' ');
    }else{
        return $new_string; 
    }   
}

$trimmed_string = trim_string($string, 120, ' ');

echo $count."<br />".substr_count($string, ' ')."<br />".$trimmed_string;

As you can see I’m trying to debug. Count returns 67, while the original number of occurrences is 86, so I know it’s working, but nothing echo’s out for $trimmed_string.

If anyone has any ideas or a better way of doing this sort of thing, lemme know!

  • 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-13T10:21:11+00:00Added an answer on June 13, 2026 at 10:21 am

    This doesn’t necessarily need to be recursive.

    function trim_string($string, $max_length, $search_char)
    {
        $string = substr($string, 0, $max_length);
        $last_index = strrpos($string, $search_char);
        return substr($string, 0, $last_index);
    }
    

    That’s a basic idea. It might give you a blank space at the end if you are chopping somewhere that’s not a space or something.

    And also, it’s not echoing because you need to return the recursion.

    if($length > $max_length){      
        $count++;
        return trim_string($new_string, 120, ' ');
    }else{
        return $new_string; 
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a view passing on information from a database: def serve_article(request, id): served_article
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.