reg exp question: removing class and style from html

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

    #1

    reg exp question: removing class and style from html

    i have a little database driven content managment system. people can
    load up html-docs. some of them use ms word as their html-editor,
    which resultes in loads of "class" and "style" attributes - like this:

    <p class="MsoNorma l">Some text</p>

    now i'd like to remove them (the attributes, not the people, that is).
    i know reg exp is the way, but somehow the solution avoids me. just
    pointing me to some more advanced tutorial (pref. in german) would
    help a lot.

    any help appreciated, micha
  • Justin Koivisto

    #2
    Re: reg exp question: removing class and style from html

    chotiwallah wrote:
    [color=blue]
    > i have a little database driven content managment system. people can
    > load up html-docs. some of them use ms word as their html-editor,
    > which resultes in loads of "class" and "style" attributes - like this:
    >
    > <p class="MsoNorma l">Some text</p>
    >
    > now i'd like to remove them (the attributes, not the people, that is).
    > i know reg exp is the way, but somehow the solution avoids me. just
    > pointing me to some more advanced tutorial (pref. in german) would
    > help a lot.
    >
    > any help appreciated, micha[/color]

    Something like this may help you:

    $pattern='`(cla ss|style)=(\\\' |\\").*\\2/Ui';
    $str=preg_repla ce($pattern,'', $str);

    Here are some of the things I've been using:
    At Regular-Expressions.info you will find a wide range of in-depth information about a powerful search pattern language called regular expressions.


    Perl regex, regular expression, extracting information, text processing


    And there are also tools like these:



    Sorry, none are German, but you may be able to translate withBabblefish:


    --
    Justin Koivisto - spam@koivi.com
    PHP POSTERS: Please use comp.lang.php for PHP related questions,
    alt.php* groups are not recommended.

    Comment

    • Josip

      #3
      Re: reg exp question: removing class and style from html

      On 12 Jul 2004 09:07:23 -0700, chotiwallah@web .de (chotiwallah) wrote:
      [color=blue]
      >i have a little database driven content managment system. people can
      >load up html-docs. some of them use ms word as their html-editor,
      >which resultes in loads of "class" and "style" attributes - like this:
      >[/color]

      This is written in JavaScript... It works great!!!!



      if ( (D.indexOf('cla ss=Mso') >= 0) || (D.indexOf('cla ss="Mso') >=
      0) ) {

      // make one line
      D = D.replace(/\r\n/g, ' ').
      replace(/\n/g, ' ').
      replace(/\r/g, ' ').
      replace(/\&nbsp\;/g,' ');

      // keep tags, strip attributes
      D = D.replace(/ class=[^\s|>]*/gi,'').

      replace(/ style=\"[^>]*\"/gi,'').
      replace(/ align=[^\s|>]*/gi,'');

      //clean up tags
      D = D.replace(/<b [^>]*>/gi,'<b>').
      replace(/<br [^>]*>/gi,'<br />').
      replace(/<i [^>]*>/gi,'<i>').
      replace(/<li [^>]*>/gi,'<li>').
      replace(/<ul [^>]*>/gi,'<ul>');

      // replace outdated tags
      D = D.replace(/<b>/gi,'<strong>').
      replace(/<\/b>/gi,'</strong>');

      // mozilla doesn't like <em> tags
      D = D.replace(/<em>/gi,'<i>').
      replace(/<\/em>/gi,'</i>');

      // kill unwanted tags
      D = D.replace(/<\?xml:[^>]*>/g, ''). // Word xml
      replace(/<\/?st1:[^>]*>/g,''). // Word SmartTags
      replace(/<\/?[a-z]\:[^>]*>/g,''). // All other funny Word
      //non-HTML stuff
      replace(/<\/?font[^>]*>/gi,''). // Disable if you want to
      //keep font formatting
      replace(/<\/?span[^>]*>/gi,' ').
      replace(/<\/?div[^>]*>/gi,' ').
      replace(/<\/?pre[^>]*>/gi,' ').
      replace(/<\/?h[1-6][^>]*>/gi,' ');

      //remove empty tags
      //D = D.replace(/<strong><\/strong>/gi,'').
      //replace(/<i><\/i>/gi,'').
      //replace(/<P[^>]*><\/P>/gi,'');

      // nuke double tags
      oldlen = D.length + 1;
      while(oldlen > D.length) {
      oldlen = D.length;
      // join us now and free the tags, we'll be free hackers,
      //we'll be free... ;-)
      D = D.replace(/<([a-z][a-z]*)> *<\/\1>/gi,' ').
      replace(/<([a-z][a-z]*)> *<([a-z][^>]*)> *<\/\1>/gi,'<$2>');
      }
      D = D.replace(/<([a-z][a-z]*)><\1>/gi,'<$1>').
      replace(/<\/([a-z][a-z]*)><\/\1>/gi,'<\/$1>');

      // nuke double spaces
      D = D.replace(/ */gi,' ');

      Comment

      • Tim Van Wassenhove

        #4
        Re: reg exp question: removing class and style from html

        In article <782d6cb.040712 0807.16d05ffc@p osting.google.c om>, chotiwallah wrote:[color=blue]
        > i have a little database driven content managment system. people can
        > load up html-docs. some of them use ms word as their html-editor,
        > which resultes in loads of "class" and "style" attributes - like this:
        >
        ><p class="MsoNorma l">Some text</p>
        >
        > now i'd like to remove them (the attributes, not the people, that is).
        > i know reg exp is the way, but somehow the solution avoids me. just
        > pointing me to some more advanced tutorial (pref. in german) would
        > help a lot.[/color]

        As soon as you get to deal with nested tags etc, regular expressions
        aren't that handy anymore. You could have a look at Tidy (a websearch
        will help you). You buffer all the output, clean it up with tidy, and
        output the cleaned up code.

        --
        Tim Van Wassenhove <http://home.mysth.be/~timvw>

        Comment

        • Michael Austin

          #5
          Re: reg exp question: removing class and style from html

          Josip wrote:
          [color=blue]
          > On 12 Jul 2004 09:07:23 -0700, chotiwallah@web .de (chotiwallah) wrote:
          >
          >[color=green]
          >>i have a little database driven content managment system. people can
          >>load up html-docs. some of them use ms word as their html-editor,
          >>which resultes in loads of "class" and "style" attributes - like this:
          >>[/color]
          >
          >
          > This is written in JavaScript... It works great!!!!
          >
          >
          >
          > if ( (D.indexOf('cla ss=Mso') >= 0) || (D.indexOf('cla ss="Mso') >=
          > 0) ) {
          >
          > // make one line
          > D = D.replace(/\r\n/g, ' ').
          > replace(/\n/g, ' ').
          > replace(/\r/g, ' ').
          > replace(/\&nbsp\;/g,' ');
          >
          > // keep tags, strip attributes
          > D = D.replace(/ class=[^\s|>]*/gi,'').
          >
          > replace(/ style=\"[^>]*\"/gi,'').
          > replace(/ align=[^\s|>]*/gi,'');
          >
          > //clean up tags
          > D = D.replace(/<b [^>]*>/gi,'<b>').
          > replace(/<br [^>]*>/gi,'<br />').
          > replace(/<i [^>]*>/gi,'<i>').
          > replace(/<li [^>]*>/gi,'<li>').
          > replace(/<ul [^>]*>/gi,'<ul>');
          >
          > // replace outdated tags
          > D = D.replace(/<b>/gi,'<strong>').
          > replace(/<\/b>/gi,'</strong>');
          >
          > // mozilla doesn't like <em> tags
          > D = D.replace(/<em>/gi,'<i>').
          > replace(/<\/em>/gi,'</i>');
          >
          > // kill unwanted tags
          > D = D.replace(/<\?xml:[^>]*>/g, ''). // Word xml
          > replace(/<\/?st1:[^>]*>/g,''). // Word SmartTags
          > replace(/<\/?[a-z]\:[^>]*>/g,''). // All other funny Word
          > //non-HTML stuff
          > replace(/<\/?font[^>]*>/gi,''). // Disable if you want to
          > //keep font formatting
          > replace(/<\/?span[^>]*>/gi,' ').
          > replace(/<\/?div[^>]*>/gi,' ').
          > replace(/<\/?pre[^>]*>/gi,' ').
          > replace(/<\/?h[1-6][^>]*>/gi,' ');
          >
          > //remove empty tags
          > //D = D.replace(/<strong><\/strong>/gi,'').
          > //replace(/<i><\/i>/gi,'').
          > //replace(/<P[^>]*><\/P>/gi,'');
          >
          > // nuke double tags
          > oldlen = D.length + 1;
          > while(oldlen > D.length) {
          > oldlen = D.length;
          > // join us now and free the tags, we'll be free hackers,
          > //we'll be free... ;-)
          > D = D.replace(/<([a-z][a-z]*)> *<\/\1>/gi,' ').
          > replace(/<([a-z][a-z]*)> *<([a-z][^>]*)> *<\/\1>/gi,'<$2>');
          > }
          > D = D.replace(/<([a-z][a-z]*)><\1>/gi,'<$1>').
          > replace(/<\/([a-z][a-z]*)><\/\1>/gi,'<\/$1>');
          >
          > // nuke double spaces
          > D = D.replace(/ */gi,' ');
          >[/color]

          The problem is that you cannot/should not ever "rely" on javascript as
          it may be turned off due it's inherent security risks.

          Comment

          • Matthias Esken

            #6
            Re: reg exp question: removing class and style from html

            chotiwallah schrieb:
            [color=blue]
            > i know reg exp is the way, but somehow the solution avoids me. just
            > pointing me to some more advanced tutorial (pref. in german) would
            > help a lot.[/color]

            regular expression, regex, TheBat!, macro, search patterns, cryptology, reguläre Ausdrücke, Regex, Kryptologie


            Any more questions? Visit the german speaking newsgroup
            de.comp.lang.ph p.misc.

            Regards,
            Matthias

            Comment

            • chotiwallah

              #7
              Re: reg exp question: removing class and style from html

              Josip <josip@sdfs.s d> wrote in message news:<dvg5f09t1 e4r23bl53oieau6 1dntiuf6ks@4ax. com>...[color=blue]
              > On 12 Jul 2004 09:07:23 -0700, chotiwallah@web .de (chotiwallah) wrote:
              >[color=green]
              > >i have a little database driven content managment system. people can
              > >load up html-docs. some of them use ms word as their html-editor,
              > >which resultes in loads of "class" and "style" attributes - like this:
              > >[/color]
              >
              > This is written in JavaScript... It works great!!!!
              >
              >
              >
              > if ( (D.indexOf('cla ss=Mso') >= 0) || (D.indexOf('cla ss="Mso') >=
              > 0) ) {
              >
              > // make one line
              > D = D.replace(/\r\n/g, ' ').
              > replace(/\n/g, ' ').
              > replace(/\r/g, ' ').
              > replace(/\&nbsp\;/g,' ');
              >
              > // keep tags, strip attributes
              > D = D.replace(/ class=[^\s|>]*/gi,'').
              >
              > replace(/ style=\"[^>]*\"/gi,'').
              > replace(/ align=[^\s|>]*/gi,'');
              >
              > //clean up tags
              > D = D.replace(/<b [^>]*>/gi,'<b>').
              > replace(/<br [^>]*>/gi,'<br />').
              > replace(/<i [^>]*>/gi,'<i>').
              > replace(/<li [^>]*>/gi,'<li>').
              > replace(/<ul [^>]*>/gi,'<ul>');
              >
              > // replace outdated tags
              > D = D.replace(/<b>/gi,'<strong>').
              > replace(/<\/b>/gi,'</strong>');
              >
              > // mozilla doesn't like <em> tags
              > D = D.replace(/<em>/gi,'<i>').
              > replace(/<\/em>/gi,'</i>');
              >
              > // kill unwanted tags
              > D = D.replace(/<\?xml:[^>]*>/g, ''). // Word xml
              > replace(/<\/?st1:[^>]*>/g,''). // Word SmartTags
              > replace(/<\/?[a-z]\:[^>]*>/g,''). // All other funny Word
              > //non-HTML stuff
              > replace(/<\/?font[^>]*>/gi,''). // Disable if you want to
              > //keep font formatting
              > replace(/<\/?span[^>]*>/gi,' ').
              > replace(/<\/?div[^>]*>/gi,' ').
              > replace(/<\/?pre[^>]*>/gi,' ').
              > replace(/<\/?h[1-6][^>]*>/gi,' ');
              >
              > //remove empty tags
              > //D = D.replace(/<strong><\/strong>/gi,'').
              > //replace(/<i><\/i>/gi,'').
              > //replace(/<P[^>]*><\/P>/gi,'');
              >
              > // nuke double tags
              > oldlen = D.length + 1;
              > while(oldlen > D.length) {
              > oldlen = D.length;
              > // join us now and free the tags, we'll be free hackers,
              > //we'll be free... ;-)
              > D = D.replace(/<([a-z][a-z]*)> *<\/\1>/gi,' ').
              > replace(/<([a-z][a-z]*)> *<([a-z][^>]*)> *<\/\1>/gi,'<$2>');
              > }
              > D = D.replace(/<([a-z][a-z]*)><\1>/gi,'<$1>').
              > replace(/<\/([a-z][a-z]*)><\/\1>/gi,'<\/$1>');
              >
              > // nuke double spaces
              > D = D.replace(/ */gi,' ');[/color]


              brilliant little script, works great, exactly what i needed :-).
              i hadn't even thought of doing the whole cleaning clientside.

              thanks a bundle to everyone, micha

              Comment

              • Michael Austin

                #8
                Re: reg exp question: removing class and style from html

                chotiwallah wrote:[color=blue]
                > Josip <josip@sdfs.s d> wrote in message news:<dvg5f09t1 e4r23bl53oieau6 1dntiuf6ks@4ax. com>...
                >[color=green]
                >>On 12 Jul 2004 09:07:23 -0700, chotiwallah@web .de (chotiwallah) wrote:
                >>
                >>[color=darkred]
                >>>i have a little database driven content managment system. people can
                >>>load up html-docs. some of them use ms word as their html-editor,
                >>>which resultes in loads of "class" and "style" attributes - like this:
                >>>[/color]
                >>
                >>This is written in JavaScript... It works great!!!!
                >>
                >>
                >>
                >> if ( (D.indexOf('cla ss=Mso') >= 0) || (D.indexOf('cla ss="Mso') >=
                >>0) ) {
                >>
                >> // make one line
                >> D = D.replace(/\r\n/g, ' ').
                >> replace(/\n/g, ' ').
                >> replace(/\r/g, ' ').
                >> replace(/\&nbsp\;/g,' ');
                >>
                >> // keep tags, strip attributes
                >> D = D.replace(/ class=[^\s|>]*/gi,'').
                >>
                >> replace(/ style=\"[^>]*\"/gi,'').
                >> replace(/ align=[^\s|>]*/gi,'');
                >>
                >> //clean up tags
                >> D = D.replace(/<b [^>]*>/gi,'<b>').
                >> replace(/<br [^>]*>/gi,'<br />').
                >> replace(/<i [^>]*>/gi,'<i>').
                >> replace(/<li [^>]*>/gi,'<li>').
                >> replace(/<ul [^>]*>/gi,'<ul>');
                >>
                >> // replace outdated tags
                >> D = D.replace(/<b>/gi,'<strong>').
                >> replace(/<\/b>/gi,'</strong>');
                >>
                >> // mozilla doesn't like <em> tags
                >> D = D.replace(/<em>/gi,'<i>').
                >> replace(/<\/em>/gi,'</i>');
                >>
                >> // kill unwanted tags
                >> D = D.replace(/<\?xml:[^>]*>/g, ''). // Word xml
                >> replace(/<\/?st1:[^>]*>/g,''). // Word SmartTags
                >> replace(/<\/?[a-z]\:[^>]*>/g,''). // All other funny Word
                >> //non-HTML stuff
                >> replace(/<\/?font[^>]*>/gi,''). // Disable if you want to
                >> //keep font formatting
                >> replace(/<\/?span[^>]*>/gi,' ').
                >> replace(/<\/?div[^>]*>/gi,' ').
                >> replace(/<\/?pre[^>]*>/gi,' ').
                >> replace(/<\/?h[1-6][^>]*>/gi,' ');
                >>
                >> //remove empty tags
                >> //D = D.replace(/<strong><\/strong>/gi,'').
                >> //replace(/<i><\/i>/gi,'').
                >> //replace(/<P[^>]*><\/P>/gi,'');
                >>
                >> // nuke double tags
                >> oldlen = D.length + 1;
                >> while(oldlen > D.length) {
                >> oldlen = D.length;
                >> // join us now and free the tags, we'll be free hackers,
                >> //we'll be free... ;-)
                >> D = D.replace(/<([a-z][a-z]*)> *<\/\1>/gi,' ').
                >> replace(/<([a-z][a-z]*)> *<([a-z][^>]*)> *<\/\1>/gi,'<$2>');
                >> }
                >> D = D.replace(/<([a-z][a-z]*)><\1>/gi,'<$1>').
                >> replace(/<\/([a-z][a-z]*)><\/\1>/gi,'<\/$1>');
                >>
                >> // nuke double spaces
                >> D = D.replace(/ */gi,' ');[/color]
                >
                >
                >
                > brilliant little script, works great, exactly what i needed :-).
                > i hadn't even thought of doing the whole cleaning clientside.
                >
                > thanks a bundle to everyone, micha[/color]

                While client-side processing/validation is a good idea, you should no longer
                rely on it as not all users turn on javascript or activeX. There are too many
                idiots out there writing malicious code that is making a good thing bad.


                --
                Michael Austin.
                Consultant - Available.
                Donations welcomed. Http://www.firstdbasource.com/donations.html
                :)

                Comment

                Working...