simple question for grabbing html tags out of string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gene.ellis@gmail.com

    simple question for grabbing html tags out of string

    Good morning. I am racking my brains over what seems like should be a
    simple question. I have a string that contains text and html.
    Basically, I would like to grab the HTML tags from the string and wrap
    some comment tags around it. Does anyone know how I would do this? I
    was thinking about using the str_replace function, but this won't work
    for certain html tags, such as <A href=#"> since the "#" won't be known
    beforehand. Maybe a simple regular expression? Thank you so much for
    your help!

    Gene

  • Daniel Tryba

    #2
    Re: simple question for grabbing html tags out of string

    gene.ellis@gmai l.com wrote:[color=blue]
    > Basically, I would like to grab the HTML tags from the string and wrap
    > some comment tags around it. Does anyone know how I would do this? I
    > was thinking about using the str_replace function, but this won't work
    > for certain html tags, such as <A href=#"> since the "#" won't be known
    > beforehand. Maybe a simple regular expression? Thank you so much for
    > your help![/color]

    You asked such a question before:
    news:1111081880 .135383.152650@ g14g2000cwa.goo glegroups.com

    But never replied on followups...

    Comment

    • gene.ellis@gmail.com

      #3
      Re: simple question for grabbing html tags out of string

      Hey Daniel,

      Thanks for the advice. The str_replace code is giving me a parse error.
      It is saying "Parse error: parse error, unexpected ';" Do you know
      where the error is taking place? Maybe a quote or something? Thank you.

      Gene

      Comment

      • NC

        #4
        Re: simple question for grabbing html tags out of string

        gene.ellis@gmai l.com wrote:[color=blue]
        >
        > I am racking my brains over what seems like should be a
        > simple question. I have a string that contains text and html.
        > Basically, I would like to grab the HTML tags from the string
        > and wrap some comment tags around it.[/color]

        Why? Wouldn't it be easiser to just strip the tags altogether?

        But if you insist on doing it your way, here you go:

        $string = "<p>This is HTML.</p>";
        $string = str_replace('<' , '<!-- <', $string);
        $string = str_replace('>' , '> -->', $string);

        Cheers,
        NC

        Comment

        • gene.ellis@gmail.com

          #5
          Re: simple question for grabbing html tags out of string

          Hey NC,

          Thanks for the advice. One question. Do you know of a way for it to
          distinguish html tags from < and > signs? For example, I would like
          this script to work on <p> but leave > and < alone. Thanks.

          Gene

          Comment

          • Nicholas Sherlock

            #6
            Re: simple question for grabbing html tags out of string

            gene.ellis@gmai l.com wrote:[color=blue]
            > Basically, I would like to grab the HTML tags from the string and wrap
            > some comment tags around it.[/color]

            Would removing the tags completely work okay?

            strip_tags($s);

            Cheers,
            Nicholas Sherlock

            Comment

            • NC

              #7
              Re: simple question for grabbing html tags out of string

              gene.ellis@gmai l.com wrote:[color=blue]
              >
              > I would like this script to work on <p>
              > but leave > and < alone.[/color]

              < and > have no place in HTML. The fact that browsers tend
              to display them correctly when they are not part of an HTML
              tag is sheer goodwill on the part of browser developers and
              should not be counted on. &lt; and &gt; should be used
              instead.

              Cheers,
              NC

              Comment

              • John Dunlop

                #8
                Re: simple question for grabbing html tags out of string

                Somebody wrote:

                [referring to str_replace, I suppose]
                [color=blue]
                > Do you know of a way for it to distinguish html tags from < and > signs?[/color]

                Use a proper parser.

                Once more and this must be a FAQ.

                --
                Jock

                Comment

                Working...