/    Sign up×
Community /Pin to ProfileBookmark

close all opened tags

I have a box where a user can input html…this html is later added to a webpage using php from mysql.

Is there a way I can have it search for all opened tags within their input..and make it close them?

For example…if a user makes a <b> tag..but doesn’t close it at the end of their html, then the rest of my text on that page, beyond their input, will be bold. Make sense?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 07.2006 — I'm sure it could be done, but it would not be trivial (see [url=http://www.w3.org/People/Raggett/tidy/]HTML Tidy[/url] for example). My recommendation might be to look into using BBCode such as is used on these forums. If you do a Google on "bbcode PHP" you'll get a few hits on the first page that include some open source php bbcode functions.
Copy linkTweet thisAlerts:
@SaveSheepauthorMar 07.2006 — alright, so heres a question for you.

instead of doing something like that, what if i were to just put a </b></i></u></font></span></div></form> at the end to close up anything that may still be left open? Those are the only tags that are allowed.

would this screw anything up?
Copy linkTweet thisAlerts:
@NogDogMar 07.2006 — It would definitely create invalid HTML. What any specific browser would do with it is undefined and therefore unpredictable.

For a simple validation, you could count each instance of each opening and closing tag, and if the totals don't match, raise an error and reject the input.
Copy linkTweet thisAlerts:
@aaronbdavisMar 07.2006 — the way compilers generally work is by using a Stack. every time the compiler finds a {, it adds it to the stack. When it finds the corresponding }, it pops the { from the front. If you can find a Stack PHP Class you could do something similar and nuance it a bit.

Instead of simply pushing or popping, do some checking for what is being popped.

e.g. suppose you have the following string:
[code=html]<p>blah blah <b>bold text</p>[/code]
you don't want to simply add the closing </b> tag as this would be invalid. you want to end up with [code=html]<p>blah blah <b>bold text</b></p>[/code]
I think this could probably be achieved by using two stacks: the first holds the unmatched open tags, and the second holds those closing tags which have been added to the text.


the above text example would run like this:
[list=1]
  • [*]feed the string into the parser

  • [*]add the <p> tag to the Open Stack

  • [*]add the <b> tag to the Open Stack

  • [*]</p> is a closing tag so check the Open stack

  • [*]it does not match to <b> to insert the </b> tag into the actual string, then add the </b> tag to the Closed Stack

  • [*]</p> is matched to <p> so pop <p> from the stack

  • [*]now look at the Open and Closed Stacks: the <b> in Open matches the </b> in Closed so pop them both.

  • [/list]
    ×

    Success!

    Help @SaveSheep 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 6.2,
    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: @meenaratha,
    tipped: article
    amount: 1000 SATS,

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

    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,
    )...