/    Sign up×
Community /Pin to ProfileBookmark

Canceling open tags and User submitted HTML handling

I am creating a user input interface (similar to this vB post box I’m typing in now), but am not sure of the best way to handle HTML input.

I would like to allow users the opportunity to use some formating tags, such as <em>, <strong>, <a>, and <blockquote>, and can accomplish this using:

[code=php]$Text = strip_tags($Text, ‘<em><strong><blockquote><a>’);[/code]

But how do I ensure that any opened tags are closed after a user’s post, and can’t be left open and “bleed” onto the rest of the page?

I could close the above tags just in case like:

[code=php]$Text .= “nr</em></strong></a></blockquote>”;[/code]

but that isn’t [I]really[/I] valid, since I’m closing tags that might not be open.

How does vB / PHPBB (and others) handle this? Or what’s the best way of ensuring that open user-allowed tags get closed?

Thanks for everything,

Nick

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@hastxDec 15.2007 — Most of these editors are controlled by javascript, placing the bb code around the text. That is why the data does not have to be submitted for the changes to take effect, because the client is handling it...then after submission php does it's things, like syntax highlighting between the "php" code, replacing tags as necessary, etc. Since there are already open & closing codes in the submission, the server doesn't really need to figure out where they go.

There are some free rich text editors available that will give you the same function as what you use here....some are all javascript and embed the html right in the submission, and others are like the one used here that use codes which will be used by the server.
Copy linkTweet thisAlerts:
@lightnbauthorDec 15.2007 — then after submission php does it's things, like syntax highlighting between the "php" code, replacing tags as necessary, etc. Since there are already open & closing codes in the submission, the server doesn't really need to figure out where they go.[/QUOTE]

What happens if the user accidentally or deliberately deletes a closing BB code tag? How does the server respond to that?

for example, the JS surrounds the word with the code: [CODE]{b}word{/b}[/CODE],but what if the user deletes the [CODE]{/b}[/CODE]?

If the server is just doing a string replace of {b} with <b>, it won't know that a closing tag is missing...
Copy linkTweet thisAlerts:
@hastxDec 15.2007 — It is a little more than a string replace in some cases (like the code tags). I guess you can test what will happen if someone deletes a closing tag...but if you think someone will delete a closing tag, you could handle it several ways in the programming (javascript validation, or a servier side that unclosed codes), but then you could go with a WYSIWYG rich text editor that displays the text as formatted. Then an accidental delete of a closing tag wont really be an issue. for example:

http://www.kevinroth.com/rte/
×

Success!

Help @lightnb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...