Hallo liebe Leute,
Regex sind irgendwie kniffelig.
Ich versuche gerade eine Funktion mit php zu schreiben, die mir Texte parst, die bbcode enthalten.
Ärger macht mir dabei die letzte Zeile: Die soll einzelne Absätze, also Absätze, die mit Zeilenumbrüchen voneinander getrennt sind, in p-Tags einschließen. Aber nicht alle: um Überschriften, Listen und Listenelemente soll natürlich kein p-Tag gesetzt werden.
Die letzte Zeile ist murkst. Allerdings bekomme ich es nicht besser hin. Kann mir jemand helfen?
Vielleicht ist es noch hilfreich, wenn ich das Ergebnis poste:
Aus (x eingefügt, damit hier die Darstellung nicht beeinflusst wird):
wird:
Ich denke mal, daran kann man gut erkennen, wo die p-Tags falsch gesetzt werden.
Vielen Dank, Grüße
Christoph
Regex sind irgendwie kniffelig.
Ich versuche gerade eine Funktion mit php zu schreiben, die mir Texte parst, die bbcode enthalten.
Ärger macht mir dabei die letzte Zeile: Die soll einzelne Absätze, also Absätze, die mit Zeilenumbrüchen voneinander getrennt sind, in p-Tags einschließen. Aber nicht alle: um Überschriften, Listen und Listenelemente soll natürlich kein p-Tag gesetzt werden.
Die letzte Zeile ist murkst. Allerdings bekomme ich es nicht besser hin. Kann mir jemand helfen?
PHP:
$text = preg_replace("/\[b\](.*)\[\/b\]/Usi", "<b>$1</b>", $text);
$text = preg_replace("/\[i\](.*)\[\/i\]/Usi", "<i>$1</i>", $text);
$text = preg_replace("/\[u\](.*)\[\/u\]/Usi", "<u>$1</u>", $text);
$text = preg_replace("/\[color=(.*)\](.*)\[\/color\]/Usi", "<span color=\"$1\">$2</span>", $text);
$text = preg_replace("/\[email=(.*)\](.*)\[\/email\]/Usi", "<a href=\"mailto:$1\">$2</a>", $text);
$text = preg_replace_callback("/\[url=(.*)\](.*)\[\/url\]/Usi", 'linkLenght', $text);
$text = str_replace(':-)',"<img src=\"/templates/" . TEMPLATE . "" . PATH_ICONS . "emoticon_smile.png\">", $text);
$text = str_replace(':-D',"<img src=\"/templates/" . TEMPLATE . "" . PATH_ICONS . "emoticon_grin.png\">", $text);
$text = str_replace(';-)',"<img src=\"/templates/" . TEMPLATE . "" . PATH_ICONS . "emoticon_wink.png\">", $text);
$text = str_replace(':-(',"<img src=\"/templates/" . TEMPLATE . "" . PATH_ICONS . "emoticon_unhappy.png\">", $text);
$text = str_replace(':-o:',"<img src=\"/templates/" . TEMPLATE . "" . PATH_ICONS . "emoticon_surprised.png\">", $text);
$text = str_replace(':-p',"<img src=\"/templates/" . TEMPLATE . "" . PATH_ICONS . "emoticon_tongue.png\">", $text);
$text = preg_replace("/\[list\](.*)\[\/list\]/Usi", "<ul>$1</ul>", $text);
$text = preg_replace("/\[list=(1|a)\](.*)\[\/list\]/Usi", "<ol type=\"$1\">$2</ol>", $text);
$text = preg_replace("/\[\*\](.*)/", '<li>$1</li>', $text);
$text = preg_replace("/\[h(1|2|3)\](.*)\[\/h(1|2|3)\]/Usi", "<h$1>$2</h$1>", $text);
$text = preg_replace("/\n(.*[^\<h(1|2|3)\>\<ul\>\<ul=(1|a)\>\<li\*\>])\n/","<p>$1</p>",$text);
return $text;
Vielleicht ist es noch hilfreich, wenn ich das Ergebnis poste:
Aus (x eingefügt, damit hier die Darstellung nicht beeinflusst wird):
Code:
[h2]Lorem ipsum[/h2]
Lorem ipsum dolor sit amet.
Cras eget mauris dui. Nam tortor tellus, auctor sed volutpat ut, ornare at est.
[h3]Itistique[/h3]
[xlist]
[x*]Cras eget mauris
[x*]Mauris ut ipsum
[x*]Ipsum dolor lorem
[x*]Lorem at mi
[x*]Mi atque diam
[x*]Diam pellentes
[x/list]
Cras eget mauris dui. Nam tortor tellus, auctor sed volutpat ut, ornare at est.
In hac habitasse platea dictumst.
wird:
Code:
<h2>Lorem ipsum</h2>
<p>Lorem ipsum dolor sit amet.
</p>Cras eget mauris dui. Nam tortor tellus, auctor sed volutpat ut, ornare at est.
<p><h3>Itistique</h3>
</p><ul>
<li>Cras eget mauris
</li>
<li>Mauris ut ipsum
</li>
<li>Ipsum dolor lorem
</li>
<li>Lorem at mi
</li>
<li>Mi atque diam
</li>
<li>Diam pellentes
</li><p></ul>
</p>Cras eget mauris dui. Nam tortor tellus, auctor sed volutpat ut, ornare at est.
In hac habitasse platea dictumst.
Ich denke mal, daran kann man gut erkennen, wo die p-Tags falsch gesetzt werden.
Vielen Dank, Grüße
Christoph