[quote]Another way may be replacing “<” with “& lt;” using PHP function
preg_replace("<", “& lt;”, $yourtag);[/quote]
- You’d be off using htmlentities() for this. It’ll catch all HTML that needs transformation, not just left brackets, and is probably more efficient than your regex.
- The regex you do show has a syntax error. In the match part of preg_replace (the first param), you need to add delimiters to the regex. It should look like preg_replace("/</", “<”, $yourtag);
This page describes the technique in what I think is a fairly straightforward way. If you don’t want a special button for selecting the text, but would rather users can simply click the textarea, the onclick handler (in red) can be made an attribute of the textarea like so:
Another option, which I think is a lot better because of its semantic clarity, is to use ranges to select text inside a
element. It’s a harder technique which isn’t easily supported across browsers, unfortunately, so I hesitate to recommend it. You would use it in much the same way as the textarea.