• Jetzt anmelden. Es dauert nur 2 Minuten und ist kostenlos!

nl2br -> nl2p

  • Ersteller Ersteller wanjapflueger
  • Erstellt am Erstellt am
W

wanjapflueger

Guest
Moin moin,

ich weiß, das Thema wurde schon tausendmal durchgekaut und ich hab auch selber schon die Lösung meines Problems gefunden, bin aber zu blöde um Sie in mein Script einzufügen... :eek:

Code:
function nl2p($string, $line_breaks = true, $xml = true) {

$string = str_replace(array('<p>', '</p>', '<br>', '<br />'), '', $string);

// It is conceivable that people might still want single line-breaks
// without breaking into a new paragraph.
if ($line_breaks == true)
    return '<p>'.preg_replace(array("/([\n]{2,})/i", "/([^>])\n([^<])/i"), array("</p>\n<p>", '$1<br'.($xml == true ? ' /' : '').'>$2'), trim($string)).'</p>';
else 
    return '<p>'.preg_replace(
    array("/([\n]{2,})/i", "/([\r\n]{3,})/i","/([^>])\n([^<])/i"),
    array("</p>\n<p>", "</p>\n<p>", '$1<br'.($xml == true ? ' /' : '').'>$2'),

    trim($string)).'</p>'; 
}
Das hier sollte aus "nl2br" dann "nl2p" machen aber ich schaffe es nicht es in meinen Code einzufügen: :(


Code:
<?php
$abfrage = "SELECT * FROM table ORDER BY id ASC";
$ergebnis = mysql_query($abfrage);
while($row = mysql_fetch_array($ergebnis)) {
    echo "<p>";
   echo nl2br($row["content"]);
    echo "</p>";
    } 
?>

Merci und vielen Dank! :)
 
PHP:
 echo "<p>";
echo nl2br($row["content"]);
echo "</p>";
ersetzen durch
PHP:
echo nl2p($row["content"]);
Das sind Grundlagen, die man auch als Anfänger hinbekommen sollte!
 
Das sind Grundlagen, die man auch als Anfänger hinbekommen sollte!

Da stand ich wohl massiv aufm Schlauch... Vielen Dank für den Denkanstoß! :)

BTW:



Der Code oben ist irgendwie müll, da er den gesamten Text zwar in <p> Text</p> einrahmt, dazwischen aber alles mit <br /> ersetzt.

Habe hier eine kürzere und bessere Variante gefunden:
Code:
function nl2p($text)
{
    $text = '<p>' . $text . '</p>';
    $text = str_replace("\r\n\r\n", "</p><p>", $text);
    $text = str_replace("\n\n", "</p><p>", $text);
    $text = str_replace("\r\n", "<br />", $text);
    $text = str_replace("\n", "<br />", $text);     
    return $text;
}
 
Zurück
Oben