How to make a simple text editor with jQuery, PHP and BBCode?

1

How can I format text in a textarea with BBCode?
Type, create a mini text editor with paragraph shortcuts, bold, center, these basic options, nothing very personalized.

    
asked by anonymous 12.03.2014 / 05:34

1 answer

6

I suggest looking for a ready-made editor, because it is a common feature that is easier than reinventing the wheel. A quick search led me to SCEditor , which seems to have everything you need. Open the site and in the demo, click on the last button ("View source"): this will switch between WYSIWYG mode. and BB code mode.

This editor is free software (MIT license), so you can integrate it into your system without any hassles. To use it, simply include the necessary scripts and stylesheets (choose the mode with BB code support):

<script type="text/javascript" src="jquery.min.js"></script>
<link rel="stylesheet" href="minified/themes/default.min.css" type="text/css" media="all" />
<script type="text/javascript" src="minified/jquery.sceditor.bbcode.min.js"></script>
<script src="languages/pt-BR.js"></script>

And call the plugin in your element ( textarea ), passing the options you want:

$("#meu_textarea").sceditor({
    plugins: "bbcode",
    style: "minified/jquery.sceditor.default.min.css",
    locale: "pt-BR"
});

Here's the API . To get the value that the user entered, you can simply use val in textarea or a specific method you can turn BB code into HTML for you or vice versa (caution: may or may not have security implications).

    
12.03.2014 / 06:50