Strip script tags and contents in them?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ajm113
    New Member
    • Jun 2007
    • 161

    Strip script tags and contents in them?

    I want too know how I can parse for <script and </script> and replace them with nothing. For some reason my preg_replace isn't working the way I planned it..

    I don't have the code of the function since I deleted it and didn't back it up.

    So can someone help me out?

    Thanks, Ajm113.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, AJM.

    My suggestion, rather than to allow all tags except <script> is to disallow all tags except for certain ones that you have decided are benign.

    PHP: strip_tags - Manual

    Comment

    • Ajm113
      New Member
      • Jun 2007
      • 161

      #3
      I think you mist read my topic, I already have that going, Its just it's not removing the code between the tags. str_replace is the function I want too work with.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        it could be that because of the added escape characters your RegEx doesn't work. you can check that by dumping the string to evaluate just before the RegEx.

        regards

        Comment

        • nathj
          Recognized Expert Contributor
          • May 2007
          • 937

          #5
          Originally posted by Ajm113
          I think you mist read my topic, I already have that going, Its just it's not removing the code between the tags. str_replace is the function I want too work with.
          AJM,

          In that case I think you need to tackle the problem in 2 steps:

          1. Using str_pos() and substr() you can define a variable that is basically the contents of the '<script>... ...</script>' tags.
          2. Using that new variable and the str_replace() function you can replace the new string with ''

          Hope that helps.
          nathj

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by Ajm113
            I think you mist read my topic, I already have that going, Its just it's not removing the code between the tags. str_replace is the function I want too work with.
            Unfortunately, Pb is not psychic, although he sometimes likes to believe it; without seeing the current code you are using, how can we point out why it isn't doing as you'd like?

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Originally posted by Markus
              Unfortunately, Pb is not psychic, although he sometimes likes to believe it;
              How would you know? ;)

              Comment

              • Ajm113
                New Member
                • Jun 2007
                • 161

                #8
                Sorry, substr, isn't going to work in this case, I actually forgot that my code returns line by line in a loop and prints it.

                So what would be a good way on telling it not too show text until it see's the </script> tag?

                Sorry, wasn't thinking, lol. I am making a mobile viewer and I am trying too cut off CSS and Javascript code.

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  Ah, Ok. Now I see what you're saying.

                  The first idea that comes to mind would be to do a regular expression along the lines of

                  [code=regexp]!<script.*?>.+? </script>![/code]

                  But it sounds like the regexp route wasn't working for you....

                  Comment

                  Working...