-1

I need to create a PHP function which takes a long paragraph and at 40 characters breaks to a new line. The catch is it should not break at exactly 40 characters because that would create obvious issues.

How would i make it so it breaks at the closest separator(.,:?!) to the 40 character mark?

Edit: The code from the thread @NicoHaase mentioned Format string after closest separator

had this snippet

$WidgetText = substr($string, 0, strrpos(substr($string, 0, 200), '.'));

which is about as far as i got to finding a solution when i searched myself. What i am wondering now is how should i modify it to break at every 40 characters?

1 Answers1

0

The way I've done it is to look at the character where you need to split, in your case position 40. If it's a separator, split there, if it's not, work backwards along the string until you find a suitable split.

a = "big long paragraph of stuff ....."
b = 40
while (b> 0) {
   if mid(a,b,1) = separator character {
      split the string here
      exit the loop }
    b--
}

pseudo-code, obviously.

droopsnoot
  • 931
  • 1
  • 7
  • 11