How to list/find html tags in text

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sindre hiåsen

    How to list/find html tags in text

    Hi,

    What I try to do is to search a text for html tags.

    Why. Because I want to use nl2br on the text, but since this command add
    br instead of all /n it is not a nice thing to add t.x. a html list inside
    the text. Of course it is possible to add the list and remove all space
    between li tags, but this is not so readable to the writer.

    What I try to do is also to add nl only in the plain text and inside li
    and td tags. So in the li case I have to find <ul> and </ul> and <table[color=blue]
    ><tr> </tr> </table>[/color]

    I did find a nice regular expression at php.net:
    function ereg_MatchedHTM LTags($tagname) {
    return
    "^(.*)(<[ \\n\\r\\t]*$tagname(>|[^>]*>))(.*)(<[ \\n\\r\\t]*/[ \\n\\r\\t]*$tagname(>|[^>]*>))(.*)$";
    }
    wich result in this.
    // The following values will be held in $Matches
    //(marked values are unintended byproducts of the expression)
    // *[0] - the entire string ($Source).
    // [1] - everything before the opening tag
    // [2] - the opening tag, including all contents (i.e.
    everything between < and >)
    // *[3] - the opening tag from end of the tag name,
    // e.g. '<body bgcolor="#00000 0">' gives '
    bgcolor="#00000 0">'
    // [4] - the tag contents (everything between the opening and
    closing tag)
    // [5] - the complete closing tag.
    // *[6] - the closing tag from the end of the tag name
    // e.g. '</body invalid text>' gives ' invalid text>'
    // [7] - everything after the closing tag.


    Nice. But it if there is two similar html tags, it returns the last one,
    not the first.

    So someone that can set me on the track of how to accomplish my goals?

    Regards, Sindre
  • Carramba

    #2
    Re: How to list/find html tags in text

    have you tryed to put verything in string sertch and abort when the first
    one found, return everyting before abort and research the rest of string?
    hope you understand were Iam doing with that

    On Mon, 13 Jun 2005 09:46:14 +0200, sindre hiåsen <sindrehi@c2i.n et> wrote:
    [color=blue]
    > Hi,
    >
    > What I try to do is to search a text for html tags.
    >
    > Why. Because I want to use nl2br on the text, but since this command add
    > br instead of all /n it is not a nice thing to add t.x. a html list
    > inside the text. Of course it is possible to add the list and remove all
    > space between li tags, but this is not so readable to the writer.
    >
    > What I try to do is also to add nl only in the plain text and inside li
    > and td tags. So in the li case I have to find <ul> and </ul> and <table[color=green]
    >> <tr> </tr> </table>[/color]
    >
    > I did find a nice regular expression at php.net:
    > function ereg_MatchedHTM LTags($tagname) {
    > return
    > "^(.*)(<[ \\n\\r\\t]*$tagname(>|[^>]*>))(.*)(<[ \\n\\r\\t]*/[ \\n\\r\\t]*$tagname(>|[^>]*>))(.*)$";
    > }
    > wich result in this.
    > // The following values will be held in $Matches
    > //(marked values are unintended byproducts of the expression)
    > // *[0] - the entire string ($Source).
    > // [1] - everything before the opening tag
    > // [2] - the opening tag, including all contents (i.e.
    > everything between < and >)
    > // *[3] - the opening tag from end of the tag name,
    > // e.g. '<body bgcolor="#00000 0">' gives '
    > bgcolor="#00000 0">'
    > // [4] - the tag contents (everything between the opening and
    > closing tag)
    > // [5] - the complete closing tag.
    > // *[6] - the closing tag from the end of the tag name
    > // e.g. '</body invalid text>' gives ' invalid
    > text>'
    > // [7] - everything after the closing tag.
    >
    >
    > Nice. But it if there is two similar html tags, it returns the last one,
    > not the first.
    >
    > So someone that can set me on the track of how to accomplish my goals?
    >
    > Regards, Sindre[/color]



    --

    Thanx in advance
    _______________ _________
    BTW. I know my english is not best in the word, so please stop bugging me
    about my spelling. And yes Iam sorry you don't understand what I mean, but
    there is no point to yell at me. Have a nice day.

    Comment

    Working...