cleibesouza Posted June 30, 2009 Share Posted June 30, 2009 I need to brake a string after every 90th char. but it can't brake the word, only brake if it's a space. So, it needs to count chars and figure out if the 90th char is a space. If yes, brake it there, if not brake it on the space before the 90th char and keep doing the same processing until the end of the string. The string will always be outputted entirely but in chunks of 90 char. Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/164264-solved-braking-a-string/ Share on other sites More sharing options...
premiso Posted June 30, 2009 Share Posted June 30, 2009 Look into substr. That will allow you to break the string and tell you if a certain char is a space or not. Quote Link to comment https://forums.phpfreaks.com/topic/164264-solved-braking-a-string/#findComment-866500 Share on other sites More sharing options...
cleibesouza Posted June 30, 2009 Author Share Posted June 30, 2009 I was able to get the first part of the string using $brakePos = strrpos($str1, " ", -(strlen($str1) - 90)); $strTemp = substr($str1, 0, $brakePos); but I'm unable to get the rest of the string. Quote Link to comment https://forums.phpfreaks.com/topic/164264-solved-braking-a-string/#findComment-866506 Share on other sites More sharing options...
cleibesouza Posted June 30, 2009 Author Share Posted June 30, 2009 Where str1 i= 'Advanced Training Institute for Mental Health Professional Evidence-based Mental Health Treatment of Traumatized A Seinfeldian Model of Ethical Reasoning: Jerry, George, Elaine, and Kramer Teach Aspirational Ethics Psychiatric Emergencies: Best Practices in the Assessment and Management of the Patient in Crisis' Quote Link to comment https://forums.phpfreaks.com/topic/164264-solved-braking-a-string/#findComment-866605 Share on other sites More sharing options...
Philip Posted June 30, 2009 Share Posted June 30, 2009 Have you looked at [http://php.net/str_split]str_split()[/url] ? Quote Link to comment https://forums.phpfreaks.com/topic/164264-solved-braking-a-string/#findComment-866610 Share on other sites More sharing options...
cleibesouza Posted June 30, 2009 Author Share Posted June 30, 2009 Yes, I was able to break it every 90chrs using str_split but can't figure out how to break on spaces and not words. Quote Link to comment https://forums.phpfreaks.com/topic/164264-solved-braking-a-string/#findComment-866618 Share on other sites More sharing options...
Alex Posted June 30, 2009 Share Posted June 30, 2009 Something like this should work: $string = 'Advanced Training Institute for Mental Health Professional Evidence-based Mental Health Treatment of Traumatized A Seinfeldian Model of Ethical Reasoning: Jerry, George, Elaine, and Kramer Teach Aspirational Ethics Psychiatric Emergencies: Best Practices in the Assessment and Management of the Patient in Crisis'; $split = str_split($string); for($i = 90;$i < count($split);$i+=90) { $temp = $i; while($split[$temp] != ' ') { $temp--; } $parts[] = $temp; } for($i = 0;$i < count($parts);$i++) { $str_parts[] = substr($string, $parts[$i - 1], $parts[$i] - $parts[$i - 1]); } print_r($str_parts); Output: Array ( [0] => Advanced Training Institute for Mental Health Professional Evidence-based Mental Health [1] => Treatment of Traumatized A Seinfeldian Model of Ethical Reasoning: Jerry, George, Elaine, [2] => and Kramer Teach Aspirational Ethics Psychiatric Emergencies: Best Practices in the ) Quote Link to comment https://forums.phpfreaks.com/topic/164264-solved-braking-a-string/#findComment-866681 Share on other sites More sharing options...
cleibesouza Posted June 30, 2009 Author Share Posted June 30, 2009 hmm.... Almost. We're missing the end of the string. More precisely this: 'Assessment and Management of the Patient in Crisis' Quote Link to comment https://forums.phpfreaks.com/topic/164264-solved-braking-a-string/#findComment-866742 Share on other sites More sharing options...
Alex Posted June 30, 2009 Share Posted June 30, 2009 hmm.... Almost. We're missing the end of the string. More precisely this: 'Assessment and Management of the Patient in Crisis' Oh, oops. That's because I didn't account for the fact that the last segment won't be 90 characters in length, try this: $string = 'Advanced Training Institute for Mental Health Professional Evidence-based Mental Health Treatment of Traumatized A Seinfeldian Model of Ethical Reasoning: Jerry, George, Elaine, and Kramer Teach Aspirational Ethics Psychiatric Emergencies: Best Practices in the Assessment and Management of the Patient in Crisis'; $split = str_split($string); for($i = 90;$i < count($split);$i+=90) { $temp = $i; while($split[$temp] != ' ') { $temp--; } $parts[] = $temp; } for($i = 0;$i < count($parts);$i++) { $str_parts[] = substr($string, $parts[$i - 1], $parts[$i] - $parts[$i - 1]); } $str_parts[] = substr($string, $parts[count($parts)-1]); print_r($str_parts); Quote Link to comment https://forums.phpfreaks.com/topic/164264-solved-braking-a-string/#findComment-866772 Share on other sites More sharing options...
Philip Posted June 30, 2009 Share Posted June 30, 2009 Oh, sorry, I didn't see you didn't want words to be broken. In that case, here's how I'd do it: <?php // the following's value should be something unique and never show in your string: define('DELIMITER', '|||'); // Your string: $string = 'Advanced Training Institute for Mental Health Professional Evidence-based Mental Health Treatment of Traumatized A Seinfeldian Model of Ethical Reasoning: Jerry, George, Elaine, and Kramer Teach Aspirational Ethics Psychiatric Emergencies: Best Practices in the Assessment and Management of the Patient in Crisis'; // wordwrap will place the delimiter at or before every 90 chars, and will not break words // we will then explode the string by our unique delimiter into an array $array = explode(DELIMITER,wordwrap($string, 90, DELIMITER, false)); // then loop through the results foreach($array as $value) { // do whatever to string... // and output it. echo $value,'<br>'; } ?> Outputs: Advanced Training Institute for Mental Health Professional Evidence-based Mental Health Treatment of Traumatized A Seinfeldian Model of Ethical Reasoning: Jerry, George, Elaine, and Kramer Teach Aspirational Ethics Psychiatric Emergencies: Best Practices in the Assessment and Management of the Patient in Crisis Quote Link to comment https://forums.phpfreaks.com/topic/164264-solved-braking-a-string/#findComment-866794 Share on other sites More sharing options...
.josh Posted July 1, 2009 Share Posted July 1, 2009 I'm afraid I don't understand this whole delimiter business. That doesn't look like anything mentioned in the OP. As far as I can tell, simply doing $string = wordwrap($string,90); should do the trick. Quote Link to comment https://forums.phpfreaks.com/topic/164264-solved-braking-a-string/#findComment-866813 Share on other sites More sharing options...
Philip Posted July 1, 2009 Share Posted July 1, 2009 I think I'm just blind. Maybe it's the brake instead of break throwing me off, but each time I read it I read the "keep doing the same processing" as process something for each line. BLAHH. Quote Link to comment https://forums.phpfreaks.com/topic/164264-solved-braking-a-string/#findComment-866817 Share on other sites More sharing options...
cleibesouza Posted July 1, 2009 Author Share Posted July 1, 2009 Thank you for all the replies and sorry for the 'braking' instead of breaking. I only noticed after I posted. The problem has been solved. Once more the forum has helped me. Thanks to all of you!! Quote Link to comment https://forums.phpfreaks.com/topic/164264-solved-braking-a-string/#findComment-867343 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.