BBcode selbst machen

  • Hallo..
    ich würde gerne auf meiner eigenen HP ein Textfeld machen af dem man wie hier im Forum die Buttons..fett, kursiv.. bilder einfügen ETC sieht
    wenn man darauf klickt sollte der jeweilige BBcode in der Textarea erscheinen.
    und oben sollte (wie hier) auf dem Button dann nich ein * zu sehensein mit dem man den Tag schließt.
    wenn man auf abschidcken geht soll der BBcode in html umgewandelt werden.
    oder wie kann ich allgemein buttons erstellen bei deren Klick im Textfeld ein bestimmter Code erscheint?
    mfg Alex

    • Anzeige

    Hallo!

    Wenn du gerade an deiner Website arbeitest oder dein aktuelles Hosting überdenkst: Wir betreiben mit NetzLiving eine Hosting-Plattform, die speziell auf Performance, Sicherheit und einfache Verwaltung ausgelegt ist.

    • ✔️ Schnelle Ladezeiten (optimiert für WordPress, WoltLab & Co.)
    • ✔️ Deutsche Server & DSGVO-konform
    • ✔️ Persönlicher Support (kein 0815-Ticket-System)

    Mehr erfahren

  • Code
    $string = "[url=http://dermazta.de/]dermazta[/url]";
    $preg = preg_replace("#\[url=(.+?)\](.+?)\[/url\]#", '[url='$1']$2[/url]', $string);
    echo $preg;


    Das soll nur ein kleines Beispiel zum Ersetzen eines Strings sein. Du kannst es so für jeden weiteren bbCode abändern. (.+?) steht für eine Zeichenkette im bbCode bzw. zwischen den bbCode-Tags (z. B. url).

  • Zitat von Green Ali3n

    den muss ich einfach nur bei mir im php-code der Seite mit der textarea einsetzen und was ist mit den buttons?
    wird der BBcode dan automatisch an das Textfeld gesendet?


    Ja, so in etwa. Es ist lediglich der Code, welcher den bbCode () umwandelt in html. Du musst das Snippet nur noch an dein Script anpassen.

    Code
    $_POST['text'] = preg_replace("#\[url=(.+?)\](.+?)\[/url\]#", '[url='$1']$2[/url]', $_POST['text']);

    So könnte die Formation angepasst aussehen (ansonsten post-Variable ändern :wink: ). Das wäre aber erst ein bbCode, den Rest kannst du auf dieselbe Art und Weise machen. Schau dir einfach die bbCodes im Forum an.
    Die Buttons werden mit Javascript zur jeweiligen Eingabe bewegt. Hier ist der Code aus dem Quelltext von ppf (von mir abgestimmt auf xhtml).

    (alt+c)";
    l_help = "Liste:

      Text

    (alt+l)";
    o_help = "Geordnete Liste:

      Text

    (alt+o)";
    p_help = "Bild einfügen: [Blockierte Grafik: http://URL_des_Bildes] (alt+p)";
    w_help = "URL einfügen: http://URL oder URL Text (alt+w)";
    a_help = "Alle offenen BBCodes schließen";
    s_help = "Schriftfarbe: Text Tip: Du kannst ebenfalls color=#FF0000 benutzen";
    f_help = "Schriftgröße: [size=x-small]Kleiner Text[/size]";
    y_help = "Zentriert: [center]Text[/center] (alt+y)";

    // Define the bbCode tags
    bbcode = new Array();
    bbtags = new Array('','','','','','','

    Zitat

    ','

    ','

    Code
    ','

    ','

      ','

    ','

      ','

    ','','http://,','[center]','[/center]');
    imageTag = false;

    // Shows the help messages in the helpline window
    function helpline(help) {
    document.post.helpbox.value = eval(help + "_help");
    }


    // Replacement for arrayname.length property
    function getarraysize(thearray) {
    for (i = 0; i < thearray.length; i++) {
    if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
    return i;
    }
    return thearray.length;
    }

    // Replacement for arrayname.push(value) not implemented in IE until version 5.5
    // Appends element to the array
    function arraypush(thearray,value) {
    thearray[ getarraysize(thearray) ] = value;
    }

    // Replacement for arrayname.pop() not implemented in IE until version 5.5
    // Removes and returns the last element of an array
    function arraypop(thearray) {
    thearraysize = getarraysize(thearray);
    retval = thearray[thearraysize - 1];
    delete thearray[thearraysize - 1];
    return retval;
    }


    function checkForm() {

    formErrors = false;

    if (document.post.message.value.length < 2) {
    formErrors = "Du musst zu Deinem Beitrag einen Text eingeben.";
    }

    if (formErrors) {
    alert(formErrors);
    return false;
    } else {
    bbstyle(-1);
    //formObj.preview.disabled = true;
    //formObj.submit.disabled = true;
    return true;
    }
    }

    function emoticon(text) {
    var txtarea = document.post.message;
    text = ' ' + text + ' ';
    if (txtarea.createTextRange && txtarea.caretPos) {
    var caretPos = txtarea.caretPos;
    caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
    txtarea.focus();
    } else {
    txtarea.value += text;
    txtarea.focus();
    }
    }

    function bbfontstyle(bbopen, bbclose) {
    var txtarea = document.post.message;

    if ((clientVer >= 4) && is_ie && is_win) {
    theSelection = document.selection.createRange().text;
    if (!theSelection) {
    txtarea.value += bbopen + bbclose;
    txtarea.focus();
    return;
    }
    document.selection.createRange().text = bbopen + theSelection + bbclose;
    txtarea.focus();
    return;
    }
    else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
    {
    mozWrap(txtarea, bbopen, bbclose);
    return;
    }
    else
    {
    txtarea.value += bbopen + bbclose;
    txtarea.focus();
    }
    storeCaret(txtarea);
    }


    function bbstyle(bbnumber) {
    var txtarea = document.post.message;

    txtarea.focus();
    donotinsert = false;
    theSelection = false;
    bblast = 0;

    if (bbnumber == -1) { // Close all open tags & default button names
    while (bbcode[0]) {
    butnumber = arraypop(bbcode) - 1;
    txtarea.value += bbtags[butnumber + 1];
    buttext = eval('document.post.addbbcode' + butnumber + '.value');
    eval('document.post.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
    }
    imageTag = false; // All tags are closed including image tags :D
    txtarea.focus();
    return;
    }

    if ((clientVer >= 4) && is_ie && is_win)
    {
    theSelection = document.selection.createRange().text; // Get text selection
    if (theSelection) {
    // Add tags around selection
    document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
    txtarea.focus();
    theSelection = '';
    return;
    }
    }
    else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
    {
    mozWrap(txtarea, bbtags[bbnumber], bbtags[bbnumber+1]);
    return;
    }

    // Find last occurance of an open tag the same as the one just clicked
    for (i = 0; i < bbcode.length; i++) {
    if (bbcode[i] == bbnumber+1) {
    bblast = i;
    donotinsert = true;
    }
    }

    if (donotinsert) { // Close all open tags up to the one just clicked & default button names
    while (bbcode[bblast]) {
    butnumber = arraypop(bbcode) - 1;
    txtarea.value += bbtags[butnumber + 1];
    buttext = eval('document.post.addbbcode' + butnumber + '.value');
    eval('document.post.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
    imageTag = false;
    }
    txtarea.focus();
    return;
    } else { // Open tags

    if (imageTag && (bbnumber != 14)) { // Close image tag before adding another
    txtarea.value += bbtags[15];
    lastValue = arraypop(bbcode) - 1; // Remove the close image tag from the list
    document.post.addbbcode14.value = "Img"; // Return button back to normal state
    imageTag = false;
    }

    // Open tag
    txtarea.value += bbtags[bbnumber];
    if ((bbnumber == 14) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
    arraypush(bbcode,bbnumber+1);
    eval('document.post.addbbcode'+bbnumber+'.value += "*"');
    txtarea.focus();
    return;
    }
    storeCaret(txtarea);
    }

    // From http://www.massless.org/mozedit/
    function mozWrap(txtarea, open, close)
    {
    var selLength = txtarea.textLength;
    var selStart = txtarea.selectionStart;
    var selEnd = txtarea.selectionEnd;
    if (selEnd == 1 || selEnd == 2)
    selEnd = selLength;

    var s1 = (txtarea.value).substring(0,selStart);
    var s2 = (txtarea.value).substring(selStart, selEnd)
    var s3 = (txtarea.value).substring(selEnd, selLength);
    txtarea.value = s1 + open + s2 + close + s3;
    return;
    }

    // Insert at Claret position. Code from
    // http://www.faqts.com/knowledge_base…id/1052/fid/130
    function storeCaret(textEl) {
    if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
    }

    // ]]>
    </script>[/code]

  • jetzt weiß ich immernoch nicht wie da irgendetwas funktioniert und schon garnicht wie ich das alles an

    Code
    echo "<form action=\"editor.php?id=$id&page=$page\" method=\"post\">
    <textarea name=\"hp\" rows=\"9\" cols=\"50\">$row->inhalt</textarea>
    
    
    
    
    <input type=\"submit\" name=\"button\" value=\"ändern\">
    </form>";


    anpassen kann :(

  • hi,

    ich hatte aus einem früherem Projekt auch einmal bbCode angewandt und hab mir dazu eine einfache PHP Funktion geschrieben.


    Sie sieht auf den ersten Blick vielleicht etwas kompliziert aus, wird aber beim genaueren betrachten einleuchtend. Im ersten Array sind die bbCodes und im zweiten die entsprechenden HTML Codes. Sie funktioniert in etwa so wie es der.bozz erklärt hat. Die Funktion kann wie folgt aufgerufen werden: echo bbcode($_POST['message']);
    Der Rest ist JavaScript.
    Hier mal ein kleines Beispiel: http://schky.sc.funpic.de/bbcode.zip
    Probier es einfach mal aus.

    Gruss Schky

    (\__/)
    (O.o )
    (> < )
    This is Bunny. Copy Bunny into your signature to help him on his way to world domination.

  • ok...dachte ich bis eben..hat doch nicht geklappt :-|
    auf dem PC und nur mit dem html-script geht es... aber wie kann ich es in ein php-script einfügen?
    ich versuche mal weiter.. aber ich muss es in soetwas einfügen:


    nur wie mache ich das dann?
    hatte es schon drinne aber da haben die buttens nicht funktioniert :(

  • hi Green Ali3n,

    weiss nicht genau was du meinst, aber ich versuch mich mal:


    Das alles benötigt nur eine Datei und ist unabhängig. Wenn der Text abgesandt wurde, wird es dann ausgewertet und das Formular verschwindet.

    Gruss Schky

    (\__/)
    (O.o )
    (> < )
    This is Bunny. Copy Bunny into your signature to help him on his way to world domination.

  • ja ich weiß :wink:
    aber mein problem ist.. ich muss es in ein echo von php bekommen...
    ich will das so machen:
    Link seite bearbeiten->Seite aussuchen->dann kommt das Textfeld in dem schon die daten die in der mySQl datenbank stehen enthalten sind
    also datei öffnen und im textfeld für welches die BBcodes sind enthält die mySQL daten.
    darum kann ich das nicht einfach als html datei nehmen :((

  • hi,

    hab den Quellcode im vorherigen Post von mir geändert, damit ich nicht fünf mal fast den gleichen Code posten muss. Hab da jetzt eingestellt, wenn $button == FALSE, kommt das Formular mit dem Inhalt von $row->inhalt. In einem echo muss das ganze nicht unbedingt sein, geht auch so z.B.:


    Musst einfach schauen, das $button und $row->inhalt gesetzt sind!

    Gruss Schky

    P.S.: Sonst meld dich mal im MSN!

    (\__/)
    (O.o )
    (> < )
    This is Bunny. Copy Bunny into your signature to help him on his way to world domination.

Jetzt mitmachen!

Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!