counting tags and writing missing tags

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ja NE

    counting tags and writing missing tags

    sometimes people just forget to close formating tag while writing
    message in forum (or enywhere else) so I wrote those lines (for <b>, <i>
    and <a> tags)

    $i_open = substr_count($t ekst, "<i>");
    $i_close = substr_count($t ekst, "</i>");
    if($i_open > $i_close) {
    $tekst = "$tekst </i>";
    }

    but... I would like to have proper nesting, ie: if someone write
    "<b>some text <i>without closing tags"
    and if my script have order like
    if($b_open > $b_close)
    if($i_open > $i_close)
    if($a_open > $a_close)

    I will have unproper nesting:
    "<b>some text <i>without closing tags </b> </i>"

    Is there some method to "see" which tag should came first?

    --
    Ja NE
    fotografija = zapisano svjetlom | fotozine = foto-e-zin

    --
  • Philip Ronan

    #2
    Re: counting tags and writing missing tags

    "Ja NE" wrote:
    [color=blue]
    > sometimes people just forget to close formating tag while writing
    > message in forum (or enywhere else) so I wrote those lines (for <b>, <i>
    > and <a> tags)
    >
    > $i_open = substr_count($t ekst, "<i>");
    > $i_close = substr_count($t ekst, "</i>");
    > if($i_open > $i_close) {
    > $tekst = "$tekst </i>";
    > }
    >
    > but... I would like to have proper nesting, ie: if someone write
    > "<b>some text <i>without closing tags"
    > and if my script have order like
    > if($b_open > $b_close)
    > if($i_open > $i_close)
    > if($a_open > $a_close)
    >
    > I will have unproper nesting:
    > "<b>some text <i>without closing tags </b> </i>"
    >
    > Is there some method to "see" which tag should came first?[/color]

    Don't use flags. They won't be of any use if tags of the same type are
    nested (e.g., "<b><b>...</b>").

    Instead, use a stack to keep a record of which tags are open.

    These functions should help:
    <http://php.net/array_push>
    <http://php.net/array_pop>

    --
    phil [dot] ronan @ virgin [dot] net



    Comment

    Working...