-1

Imagine a very long article with no line breaks. It's just like

sentence 1. sentence 2. sentence 3. sentence 4. sentence 5. sentence 6. sentence 7. 
sentence 8. sentence 9. sentence 10. sentence 11. sentence 12. sentence 13. sentence 14. 
sentence 15. sentence 16. sentence 17. sentence 18. sentence 19. sentence 20. sentence 
21. sentence 22. sentence 23. sentence 24. sentence 25.

I want to add line breaks after every 5 sentences. Or after every 5th ". "

How can I do that ?

user198989
  • 4,574
  • 19
  • 66
  • 95
  • Tried
     tags, white-space:pre-wrap; word-wrap: break-word;. Is that the reason to give -1 ?
    – user198989 Aug 28 '12 at 23:44
  • 1
    also, wouldn't it be
    not
    – Emile Aug 28 '12 at 23:45
  • 1
    Both works fine. Doesn't matter. – user198989 Aug 28 '12 at 23:45
  • @user198989 probably -

    is semantically incorrect. Can you make a jsfiddle with '

    yourtext

    ' and tell us what's wrong with it? e.g. http://jsfiddle.net/ytEuB/
    – valentinas Aug 28 '12 at 23:45
  • 1
    What if _Mr. Smith went to Washington to meet Mr. Jones._? That's three `.` in one sentence – Michael Berkowski Aug 28 '12 at 23:46
  • @Emile - http://stackoverflow.com/questions/1946426/html-5-is-it-br-br-or-br – Paul Dessert Aug 28 '12 at 23:46
  • I can recommend [PCRE with -e modifier](http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php) – Jan Turoň Aug 28 '12 at 23:47
  • 1
    This site is designed to help people with their code - it's not a place where people will do your work for you. You should include some code to show that you've at least attempted to solve this yourself. I imagine whoever gave you the -1 is because there's nothing in your question to show that you've tried to solve this already - it currently reads like you want someone to do it for you. – andrewsi Aug 28 '12 at 23:47

3 Answers3

2

It's a really bad way, I think this can be done with regex, but anyway.

<?php
$str = "a.b.c.d.e.f.g.h.i.j.";
$arr = explode(".", $str);
$new_str = "";
$j = 1;
foreach($arr as $arr_el) {
    $new_str .= $arr_el.".";
    if($j % 5 == 0) {
        $new_str .= "<br/><br/>";
    }
    $j++;
}

echo $new_str;
Sergey Telshevsky
  • 12,077
  • 6
  • 55
  • 78
2
$str="long sentence. long sentence. ";
$lines=explode('. ', $str); // Break sentence with ". "
$i=1;
    foreach($lines as $l){
    echo $lines;
    if ($i%5==0){echo "<br /><br />";}
i++;
}

May work for you, if the sentence is that simple.

Moe Tsao
  • 1,054
  • 6
  • 9
1

There are two pieces to this puzzle:

  1. Breaking up your input text into sentences
  2. Inserting the required text every 5 sentences

I 'm not going to tackle #1 because it usually turns out that "after every fifth dot" does not actually cut it in practice, so everyone reaches for their regular expressions and before you know it there's a menagerie of unreadable regexes you are supposed to make an informed choice from. I 'll just say that splitting on periods can be done as easy as explode(".", $string) and leave it at that.

The second problem is thankfully easier. Assuming that $sentences is an array of strings where each string is one sentence (including the ending period), you can do it like this:

$chunks = array_chunk($sentences, 5);
foreach($chunks as &$chunk) {
    $chunk = implode(' ', $chunk);
}
$result = implode("<br><br>", $chunks);

I admit that I was suckered into giving this code because it's not every day that you get to use array_chunk.

Jon
  • 428,835
  • 81
  • 738
  • 806