Question for the Gurus - preg_replace and HTML

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ihatzi@hotmail.com

    #1

    Question for the Gurus - preg_replace and HTML

    Hey all!

    I am having a bit of trouble getting this to work.

    You all are probably familiar with the code that strips HTML tags:
    $PageText = preg_replace("/<.+?>/","",$WebPa ge);

    What I need to do is the exact oposite, I want to strip the page text
    and keep the HTML tags.

    Lets say I have: "Hello <b>World</b>"
    I want to get: "<b></b>"

    Thanks in advance
    Ion

  • Stian Berger

    #2
    Re: Question for the Gurus - preg_replace and HTML

    On 9 Feb 2005 09:25:52 -0800, <ihatzi@hotmail .com> wrote:
    [color=blue]
    > Hey all!
    >
    > I am having a bit of trouble getting this to work.
    >
    > You all are probably familiar with the code that strips HTML tags:
    > $PageText = preg_replace("/<.+?>/","",$WebPa ge);
    >
    > What I need to do is the exact oposite, I want to strip the page text
    > and keep the HTML tags.
    >
    > Lets say I have: "Hello <b>World</b>"
    > I want to get: "<b></b>"
    >
    > Thanks in advance
    > Ion
    >[/color]


    preg_match_all( "/<[^>]+>/",$WebPage,$mat ch);

    You will then have all the tags in a nice array.
    Just implode the array if you want it in a string.

    --
    Stian

    Comment

    • Jerry Sievers

      #3
      Re: Question for the Gurus - preg_replace and HTML

      ihatzi@hotmail. com writes:
      [color=blue]
      > You all are probably familiar with the code that strips HTML tags:
      > $PageText = preg_replace("/<.+?>/","",$WebPa ge);
      >
      > What I need to do is the exact oposite, I want to strip the page text
      > and keep the HTML tags.[/color]

      Ok sinple enough. You'll have to test this yourself though.

      replace >.*?< with ><

      In other words, since content will be found between tags of whatever
      sort, replace that with only the delimiters and you should have it.
      Not optomized and will do replacements where not really needed. Have
      fun.

      Get it right and go mad trying most optimal (er, elegant?) solution
      using negative lookahead/behind!

      HTH

      --
      -------------------------------------------------------------------------------
      Jerry Sievers 305 854-3001 (home) WWW ECommerce Consultant
      305 321-1144 (mobile http://www.JerrySievers.com/

      Comment

      • ihatzi@hotmail.com

        #4
        Re: Question for the Gurus - preg_replace and HTML

        Just tried it,

        preg_match_all( "/<[^>]+>/", $WebPage,$match ) seems to be creating an
        array "$match" with one element that simply says "Array" in pos [0]. I
        know I must be doing something wrong (usually obvious).

        Any ideas?

        Comment

        • ihatzi@hotmail.com

          #5
          Re: Question for the Gurus - preg_replace and HTML

          The Following:

          preg_match_all( "/<[^>]+>/", "Hello<b>Wo rld</b>Bye", $match);
          echo "00: " . $match[0] . "<br>";
          echo "01: " . $match[1] . "<br>";
          echo "02: " . $match[2] . "<br>";

          Is Giving Me:

          00: Array
          01:
          02:

          Not sure what is wrong.
          Thanks in advance for help!
          Ion

          Comment

          • Stian Berger

            #6
            Re: Question for the Gurus - preg_replace and HTML

            On 9 Feb 2005 12:06:39 -0800, <ihatzi@hotmail .com> wrote:
            [color=blue]
            > Just tried it,
            >
            > preg_match_all( "/<[^>]+>/", $WebPage,$match ) seems to be creating an
            > array "$match" with one element that simply says "Array" in pos [0]. I
            > know I must be doing something wrong (usually obvious).
            >
            > Any ideas?
            >[/color]

            When you use preg_match, and want to store the matched results you get
            the complete match in $match[0], and stuff matched in capturing
            parantheses is placed in $match[1] and up. As we are not using any
            capturing parantheses you only get one array. The info you want is
            inside this again. To see this more clearly try print_r($match) ;,
            or you could implode('',$mat ch[0]);

            The solution to Jerry posted earlier is easier if you
            want the tags directly in a string.

            --
            Stian

            Comment

            • ihatzi@hotmail.com

              #7
              Re: Question for the Gurus - preg_replace and HTML

              Thanks Guys, I got it to work.

              The help is GREATLY appreciated!

              Ion

              Comment

              • John Dunlop

                #8
                Re: Question for the Gurus - preg_replace and HTML

                [Anonymous -- J.D.] wrote:
                [color=blue]
                > You all are probably familiar with the code that strips HTML tags:[/color]

                Parsing arbitrary HTML should be done with a parser rather
                than a single, ad hoc regular expression. Predetermined
                HTML, on the other hand, can be parsed by a regular
                expression because you know beforehand its exact form.
                [color=blue]
                > $PageText = preg_replace("/<.+?>/","",$WebPa ge);[/color]

                Performs pretty much the same function as strip_tags, which
                for the most part is too simplistic to be of any practical
                worth.

                --
                Jock

                Comment

                • Chung Leong

                  #9
                  Re: Question for the Gurus - preg_replace and HTML

                  <ihatzi@hotmail .com> wrote in message
                  news:1107969952 .924985.205970@ c13g2000cwb.goo glegroups.com.. .[color=blue]
                  > Hey all!
                  >
                  > I am having a bit of trouble getting this to work.
                  >
                  > You all are probably familiar with the code that strips HTML tags:
                  > $PageText = preg_replace("/<.+?>/","",$WebPa ge);
                  >
                  > What I need to do is the exact oposite, I want to strip the page text
                  > and keep the HTML tags.
                  >
                  > Lets say I have: "Hello <b>World</b>"
                  > I want to get: "<b></b>"
                  >
                  > Thanks in advance
                  > Ion
                  >[/color]

                  If your code is meant to handle any HTML pages, then be mindful of
                  Javascript. Code snippets have to be removed first, for otherwise code that
                  happens to lie between a < and > operator would be incorrectly interpreted
                  as being a tag.

                  Something like this should do it:

                  preg_replace("/<script.*?>.*?< \/script\s*>/", "", $webpage);



                  Comment

                  • Stian Berger

                    #10
                    Re: Question for the Gurus - preg_replace and HTML

                    On Wed, 9 Feb 2005 21:06:29 -0500, Chung Leong <chernyshevsky@ hotmail.com>
                    wrote:
                    [color=blue]
                    > <ihatzi@hotmail .com> wrote in message
                    > news:1107969952 .924985.205970@ c13g2000cwb.goo glegroups.com.. .[color=green]
                    >> Hey all!
                    >>
                    >> I am having a bit of trouble getting this to work.
                    >>
                    >> You all are probably familiar with the code that strips HTML tags:
                    >> $PageText = preg_replace("/<.+?>/","",$WebPa ge);
                    >>
                    >> What I need to do is the exact oposite, I want to strip the page text
                    >> and keep the HTML tags.
                    >>
                    >> Lets say I have: "Hello <b>World</b>"
                    >> I want to get: "<b></b>"
                    >>
                    >> Thanks in advance
                    >> Ion
                    >>[/color]
                    >
                    > If your code is meant to handle any HTML pages, then be mindful of
                    > Javascript. Code snippets have to be removed first, for otherwise code
                    > that
                    > happens to lie between a < and > operator would be incorrectly
                    > interpreted
                    > as being a tag.
                    >
                    > Something like this should do it:
                    >
                    > preg_replace("/<script.*?>.*?< \/script\s*>/", "", $webpage);
                    >[/color]

                    If javascript is a problem, and you don't want to remove them,
                    there is a solution:
                    preg_match_all( "/<(?:(?=script>) |(?!(?!.*?<scri pt>).*?<\/script>))[^>]+>/s",$webpage,$ma tch);
                    It's ugly, I know, but i think it might work.
                    It says something like:
                    <(?:(?=script >) #Match tag start followed by script
                    | #Or
                    (?!(?!.*?<scrip t>).*?<\/script>))[^>]+> #Tags wich is not within script

                    I ran this on an example:
                    <html>
                    <body>
                    <script>
                    var i = 10;
                    if(i<10) {
                    } else if(i>10) {
                    } else {
                    }
                    </script>
                    This <b>is a</b> Test
                    </body>
                    </html>

                    And the output is:
                    [0] => Array
                    (
                    [0] => <html>
                    [1] => <body>
                    [2] => <script>
                    [3] => </script>
                    [4] => <b>
                    [5] => </b>
                    [6] => </body>
                    [7] => </html>
                    )
                    Notice that you don't get a tag called "<10) {} else if(i>".

                    --
                    Stian

                    Comment

                    Working...