Langen Text (variable und in nur einer Zeile) lesbar formatieren

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Langen Text (variable und in nur einer Zeile) lesbar formatieren

    Hi,
    ich habe verschiedene Text die alle in nur einer Zeile stehen = nicht gerade gut lesbar :/

    Bsp.:
    On the other hand, we denounce with righteous indignation and
    dislike men who are so beguiled and demoralized by the charms of
    pleasure of the moment, so blinded by desire, that they cannot foresee
    the pain and trouble that are bound to ensue; and equal blame belongs to
    those who fail in their duty through weakness of will, which is the
    same as saying through shrinking from toil and pain. These cases are
    perfectly simple and easy to distinguish. In a free hour, when our power
    of choice is untrammelled and when nothing prevents our being able to
    do what we like best, every pleasure is to be welcomed and every pain
    avoided. But in certain circumstances and owing to the claims of duty or
    the obligations of business it will frequently occur that pleasures
    have to be repudiated and annoyances accepted. The wise man therefore
    always holds in these matters to this principle of selection: he rejects
    pleasures to secure other greater pleasures, or else he endures pains
    to avoid worse pains.On the other hand, we denounce with righteous
    indignation and dislike men who are so beguiled and demoralized by the
    charms of pleasure of the moment, so blinded by desire, that they cannot
    foresee the pain and trouble that are bound to ensue; and equal blame
    belongs to those who fail in their duty through weakness of will, which
    is the same as saying through shrinking from toil and pain. These cases
    are perfectly simple and easy to distinguish. In a free hour, when our
    power of choice is untrammelled and when nothing prevents our being able
    to do what we like best, every pleasure is to be welcomed and every
    pain avoided. But in certain circumstances and owing to the claims of
    duty or the obligations of business it will frequently occur that
    pleasures have to be repudiated and annoyances accepted. The wise man
    therefore always holds in these matters to this principle of selection:
    he rejects pleasures to secure other greater pleasures, or else he
    endures pains to avoid worse pains
    Meine Idee ist nun mit hilfe von PHP nach den Punkten <br/> zu adden.
    Am besten wäre: - setzte <br /> nach X Sätzen.

    X = random Zahl zwischen 2-4

    Hat jemand eine Idee wie ich das in PHP umsetzen kann?
    Freue mich über jede Antwort :)
  • Hi

    Variante 1
    * Also erstmal explodest du nach Punkt und Leerzeichen
    * dann iterierst du über das Array.. und bei jedem vierten (oder random) gibst du ein <br /> aus

    Variante 2
    * alternativ sollte es auch mit preg_replace und dem E modifier gehen. Dort kannst du eine Callback Methode angeben, die nur bei rand(1,4)==1 ein Leerzeichen ausgibt
  • Naja, hier Variante 1

    Quellcode

    1. // replace existing linebreaks through spaces
    2. $text = preg_replace("/[\r\n]?\n/", ' ', $text);
    3. // replace sentence end (dot, questionmark, ...) followed by space
    4. $text = preg_split('/[\.\?!] /', $text);
    5. foreach($text as $i => $sentence) {
    6. echo $sentence;
    7. if ($i % 4 == 0) {
    8. echo '<br />';
    9. }
    10. }
    Alles anzeigen


    Variante 2 wäre eher eine Spielerei gewesen ;)