Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/164264-solved-braking-a-string/
Share on other sites

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'

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 )

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);

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.