Preg Replace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bruno Rafael Moreira de Barros

    Preg Replace

    I have a file with <html><head><me ta something...><m eta something
    else...>. I need to remove that text.

    I would use str_ireplace, but the contents of the two metas change
    with the file.

    I know preg_replace allows you to do that, but how? I have tried to
    learn regular expressions, but I am having serious problems doing so.

    Thanks in advance.
  • Albert Nick

    #2
    Re: Preg Replace

    On 4ÔÂ13ÈÕ, ÉÏÎç2ʱ36·Ö, Bruno Rafael Moreira de Barros
    <brunormbar...@ gmail.comwrote:
    I have a file with <html><head><me ta something...><m eta something
    else...>. I need to remove that text.
    >
    I would use str_ireplace, but the contents of the two metas change
    with the file.
    >
    I know preg_replace allows you to do that, but how? I have tried to
    learn regular expressions, but I am having serious problems doing so.
    >
    Thanks in advance.
    What you want to do is just removing HTML tags?

    Comment

    • Alexey Kulentsov

      #3
      Re: Preg Replace

      Bruno Rafael Moreira de Barros wrote:
      I have a file with <html><head><me ta something...><m eta something
      else...>. I need to remove that text.
      >
      I would use str_ireplace, but the contents of the two metas change
      with the file.
      >
      I know preg_replace allows you to do that, but how? I have tried to
      learn regular expressions, but I am having serious problems doing so.
      >
      Thanks in advance.
      $without_meta=p reg_replace('/<meta[^>]*>/i','',$with_met a);

      Comment

      • Tim Roberts

        #4
        Re: Preg Replace

        Alexey Kulentsov <akul@inbox.ruw rote:
        >Bruno Rafael Moreira de Barros wrote:
        >I have a file with <html><head><me ta something...><m eta something
        >else...>. I need to remove that text.
        >>
        >I would use str_ireplace, but the contents of the two metas change
        >with the file.
        >>
        >I know preg_replace allows you to do that, but how? I have tried to
        >learn regular expressions, but I am having serious problems doing so.
        >>
        >Thanks in advance.
        >
        >$without_meta= preg_replace('/<meta[^>]*>/i','',$with_met a);
        <html>
        <head>
        <head<meta name="descripti on" content="This has a <brtag">

        Whoops...
        --
        Tim Roberts, timr@probo.com
        Providenza & Boekelheide, Inc.

        Comment

        • Alexey Kulentsov

          #5
          Re: Preg Replace

          Tim Roberts wrote:
          Alexey Kulentsov <akul@inbox.ruw rote:
          >
          >Bruno Rafael Moreira de Barros wrote:
          >>I have a file with <html><head><me ta something...><m eta something
          >>else...>. I need to remove that text.
          >>>
          >>I would use str_ireplace, but the contents of the two metas change
          >>with the file.
          >>>
          >>I know preg_replace allows you to do that, but how? I have tried to
          >>learn regular expressions, but I am having serious problems doing so.
          >>>
          >>Thanks in advance.
          >$without_meta= preg_replace('/<meta[^>]*>/i','',$with_met a);
          >
          <html>
          <head>
          <head<meta name="descripti on" content="This has a <brtag">
          >
          Whoops...
          Incorrect HTML.
          Here must be <meta name="descripti on" content="This has a &lt;br&gt; tag">

          Comment

          • Rik Wasmus

            #6
            Re: Preg Replace

            On Fri, 18 Apr 2008 10:36:27 +0200, Alexey Kulentsov <akul@inbox.ruw rote:
            Tim Roberts wrote:
            >Alexey Kulentsov <akul@inbox.ruw rote:
            >>
            >>Bruno Rafael Moreira de Barros wrote:
            >>>I have a file with <html><head><me ta something...><m eta something
            >>>else...>. I need to remove that text.
            >>>>
            >>>I would use str_ireplace, but the contents of the two metas change
            >>>with the file.
            >>>>
            >>>I know preg_replace allows you to do that, but how? I have tried to
            >>>learn regular expressions, but I am having serious problems doing so.
            >>>>
            >>>Thanks in advance.
            >>$without_meta =preg_replace('/<meta[^>]*>/i','',$with_met a);
            > <html>
            > <head>
            > <head<meta name="descripti on" content="This has a <brtag">
            > Whoops...
            Incorrect HTML.
            Here must be <meta name="descripti on" content="This has a &lt;br&gt;
            tag">
            Indeed, and how many sites have incorrect HTML? A lot more then fully
            valid ones... Which is why I'm going te repeat the mantra: "Use a parser
            for HTML manipulation or inspectation, not Regex". DOM comes to mind, very
            nice package.
            --
            Rik Wasmus

            Comment

            • Alexey Kulentsov

              #7
              Re: Preg Replace

              Rik Wasmus wrote:
              Indeed, and how many sites have incorrect HTML? A lot more then fully
              valid ones... Which is why I'm going te repeat the mantra: "Use a parser
              for HTML manipulation or inspectation, not Regex". DOM comes to mind,
              very nice package.
              I think I am in big trouble: sometimes I use Regex-based parser, for
              HTML too. :) I know about problem with incorrect attributes and can
              write regexp to deal with it. Yes, this is much more easy to load file
              into DOM document using loadHTML() then to write complex regexp dealing
              with all these incorrect cases.

              Comment

              • John Dunlop

                #8
                Re: Preg Replace

                Alexey Kulentsov:
                Tim Roberts wrote:
                >
                <meta name="descripti on" content="This has a <brtag">
                >
                Incorrect HTML.
                No it isn't.

                (Yet *another* reason not to reinvent this particular wheel.)

                --
                Jock

                Comment

                • AnrDaemon

                  #9
                  Re: Preg Replace

                  Greetings, Alexey Kulentsov.
                  In reply to Your message dated Friday, April 18, 2008, 12:36:27,
                  >>>I have a file with <html><head><me ta something...><m eta something
                  >>>else...>. I need to remove that text.
                  >>>>
                  >>>I would use str_ireplace, but the contents of the two metas change
                  >>>with the file.
                  >>>>
                  >>>I know preg_replace allows you to do that, but how? I have tried to
                  >>>learn regular expressions, but I am having serious problems doing so.
                  >>>>
                  >>>Thanks in advance.
                  >>$without_meta =preg_replace('/<meta[^>]*>/i','',$with_met a);
                  >>
                  > <html>
                  > <head>
                  > <head<meta name="descripti on" content="This has a <brtag">
                  >>
                  >Whoops...
                  Incorrect HTML.
                  Here must be <meta name="descripti on" content="This has a &lt;br&gt; tag">
                  According to standard, you do not need to escape ">", only "<".


                  --
                  Sincerely Yours, AnrDaemon <anrdaemon@free mail.ru>

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: Preg Replace

                    AnrDaemon wrote:
                    Greetings, Alexey Kulentsov.
                    In reply to Your message dated Friday, April 18, 2008, 12:36:27,
                    >
                    >>>>I have a file with <html><head><me ta something...><m eta something
                    >>>>else...>. I need to remove that text.
                    >>>>>
                    >>>>I would use str_ireplace, but the contents of the two metas change
                    >>>>with the file.
                    >>>>>
                    >>>>I know preg_replace allows you to do that, but how? I have tried to
                    >>>>learn regular expressions, but I am having serious problems doing so.
                    >>>>>
                    >>>>Thanks in advance.
                    >>>$without_met a=preg_replace( '/<meta[^>]*>/i','',$with_met a);
                    >> <html>
                    >> <head>
                    >> <head<meta name="descripti on" content="This has a <brtag">
                    >>>
                    >>Whoops...
                    > Incorrect HTML.
                    >Here must be <meta name="descripti on" content="This has a &lt;br&gt; tag">
                    >
                    According to standard, you do not need to escape ">", only "<".
                    >
                    >
                    Wrong (again). That's why there is &gt; Otherwise the <meta tag may
                    end prematurely.

                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===

                    Comment

                    • AnrDaemon

                      #11
                      Re: Preg Replace

                      Greetings, Jerry Stuckle.
                      In reply to Your message dated Sunday, June 8, 2008, 21:42:36,
                      >>>>>I have a file with <html><head><me ta something...><m eta something
                      >>>>>else...> . I need to remove that text.
                      >>>>>>
                      >>>>>I would use str_ireplace, but the contents of the two metas change
                      >>>>>with the file.
                      >>>>>>
                      >>>>>I know preg_replace allows you to do that, but how? I have tried to
                      >>>>>learn regular expressions, but I am having serious problems doing so.
                      >>>>>>
                      >>>>>Thanks in advance.
                      >>>>$without_me ta=preg_replace ('/<meta[^>]*>/i','',$with_met a);
                      >>> <html>
                      >>> <head>
                      >>> <head<meta name="descripti on" content="This has a <brtag">
                      >>>>
                      >>>Whoops...
                      >> Incorrect HTML.
                      >>Here must be <meta name="descripti on" content="This has a &lt;br&gt; tag">
                      >>
                      >According to standard, you do not need to escape ">", only "<".
                      Wrong (again). That's why there is &gt; Otherwise the <meta tag may
                      end prematurely.
                      Welcome to reality!



                      --
                      Sincerely Yours, AnrDaemon <anrdaemon@free mail.ru>

                      Comment

                      • John Dunlop

                        #12
                        Re: Preg Replace

                        AnrDaemon:
                        Greetings, Alexey Kulentsov.
                        >
                        Here must be <meta name="descripti on" content="This has a &lt;br&gt; tag">
                        >
                        According to standard, you do not need to escape ">", only "<".
                        I don't know what "standard" you are talking about, but the HTML
                        specification allows both "<" and ">" in the content attribute.
                        Content is defined as CDATA, so pretty much anything goes. If you are
                        talking about the XHTML specification, then yes, "<" needs to be
                        escaped but ">" doesn't.

                        --
                        Jock

                        Comment

                        Working...