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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:47:25+00:00 2026-06-18T22:47:25+00:00

I used some codes to limit displaying characters from a paragraph in a while

  • 0

I used some codes to limit displaying characters from a paragraph in a while loop.

This is my code:

//mysqli_stmt_fetch ($stmt);    
while (mysqli_stmt_fetch($stmt)) {

      $position=70; // Define how many characters you want to display.      
      // Find what is the last character.
      $subject = substr($subjects,$position,1);

      if($subject !=" "){
         while($subject !=" "){
            $i=1;
            $position=$position+$i;
            $subject = substr($subjects,$position,1); 
         }
      }     
      $subject = substr($subjects,0,$position); 

      echo '<h4><span>Subjects / </span>'.$subject.'.....</h4>';   
}

My problem is when running this script it take long time to run. If I discrease $position’s value then executing script quickly and if I increase $position’s value it take long time to execute.

if its $position=80 I can’t get it to work. Actually its not executing at all. My windows Task Manager display it is using Physical Memory 100%.

Can anybody tell me what is the reason for this?

Thank you.

  • 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-18T22:47:27+00:00Added an answer on June 18, 2026 at 10:47 pm

    If I understand correctly you wish to return first 70 characters of string, but cut it off at the first space character.

    You could use this:

    function get_snippet($subject, $count) {
    
        // if string length is already under the desired char count
        if (strlen($subject) <= $count) return $subject;
    
        // find first space char after desired position
        $pos = strpos($subject, ' ', $count);
    
        if ($pos === false) return $subject;   // no space, must be end of the string so return entire text
        else return substr($subject, 0, $pos) . '...'; // return all chars up to first space
    }
    

    Calling it:

    $newtext = get_snippet($subjects, 70);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some objective-c code I'm converting from iPhone to iPad. CFGregorianDate is used
I used a CSS reset to reset some commonly-used items. The code is this:
I need to select some subjects from db.. I used below code to select
I'm having a problem with some code that used to work in PHP 4.X
// .net 2.0 and vs2005 used. I find some code below. I am not
I have some legacy code that was used to monitor my applications cpu,memory etc
I'm reading some ruby code, and see 0..size and 0...size are used in similar
I'm working on some finite element analysis code, and one of our commonly used
Altough I've known Code Contracts for some time, as I've used it a bit
Does some PDO::PARAM_??? exist which can be used for dates or timestamps? Sample code:

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.