thara Posted February 16, 2013 Share Posted February 16, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274566-my-script-use-more-memory-to-run/ Share on other sites More sharing options...
scootstah Posted February 16, 2013 Share Posted February 16, 2013 I can't really figure out what you're trying to do. Are you just trying to truncate the string to a specificed length? If so, you're making it far more complicated than it needs to be. Also, what is $subjects? Where does that come from? Quote Link to comment https://forums.phpfreaks.com/topic/274566-my-script-use-more-memory-to-run/#findComment-1412817 Share on other sites More sharing options...
Barand Posted February 16, 2013 Share Posted February 16, 2013 You access characters in a string as though it were an array of characters. $position=70; // Define how many characters you want to display. if (strlen($subjects) > $position) { while ($subjects[$position-1] != ' ') { --$position; } $subject = substr($subjects,0,$position); echo '<h4><span>Subjects / </span>'.$subject.'....</h4>'; } else { echo '<h4><span>Subjects / </span>'.$subjects.'</h4>'; } Quote Link to comment https://forums.phpfreaks.com/topic/274566-my-script-use-more-memory-to-run/#findComment-1412834 Share on other sites More sharing options...
PaulRyan Posted February 16, 2013 Share Posted February 16, 2013 (edited) <?PHP //mysqli_stmt_fetch ($stmt); while (mysqli_stmt_fetch($stmt)) { $subject = $stmt['subject']; $cutOff = 70; //### Define how many characters you want to display. if(strlen($subject) > $position) { $findSpace = strpos($subject, ' ', $cutOff); //### Find first space after cutoff point $subject = substr($subject, 0, $findSpace); //### Get the substr of subjects to display } echo '<h4><span>Subjects / </span>'. $subject .'.....</h4>'; } ?> Edited February 16, 2013 by PaulRyan Quote Link to comment https://forums.phpfreaks.com/topic/274566-my-script-use-more-memory-to-run/#findComment-1412836 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.