WYSIWYG selected text formatting

Okay. Here is the deal.
I’m trying to make a WYSIWYG editor in flash. But I just don’t understand, how to add tags to selected text. Flash has it’s own tags to, and there is even an example in the samples folder, but still, it doesent really help me, because the text will be sent to php, where it needs to have tags.

So. Could anyone explain to me, how to add something before and after selected text, in the input text box.

Example.
You select “selected”, press button B and it adds <b></b> to “selected”, so final “product” will be “<b>selected</b>”, ofcourse without quotation marks. : )

Thanks.