0
votes

A string has a very long text. How is it possible to make auto line breaks after 100 words without cutting words and to take care of commas and dots. A sentence should not be broken. Only line break after dots. The line breaks should be added, when in the complete text are NO br tags.

Example:

$string = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.';

Output:

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.

It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

I tried wordwrap but it's too simple.

1
Split string by spaces. Add items one by one to the output while keeping a counter. If the counter >= 100, check each word if it ends on a dot. If so, output a break and reset the counter to 0.GolezTrol

1 Answers

0
votes

Split string by spaces. Add items one by one to the output while keeping a counter. If the counter >= 100, check each word if it ends on a dot. If so, output a break and reset the counter to 0.

So, something like this:

<?php
  $string = 'Your chunk of. Lipsum.';

  $words = explode(' ', $string);

  echo '<p>';

  $counter = 0;
  foreach ($words as $word) {
    echo $word . ' ';
    if (++$counter >= 100) {
      if (substr($word, -1) === '.') {
        echo "</p>\n\n<p>"; // End the paragraph and start a new one.
        $counter = 0;
      }
    }
  }
  echo '</p>';