Hello there. I had a quick question. I wanted to allow people on my website to copy an html tag (this way they can put my banner on their websites). Nevertheless, I am not sure what the best way is to display the tag on my page. Would anyone have any advice?
Some people put the tag in textarea.
Another way may be replacing “<” with “& lt;” using PHP function
preg_replace("<", “& lt;”, $yourtag);
no space between “&” and “lt;”
Thank you very much for your response!
I would like to place it in a text area, I think that would look cleaner. Would you have any advice on the best way to do this?
[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.
Excellent advice. Thank you!!!