Hello

I need to know if a given string contains HTML tags or not. Is there a way to detect HTML tags in the string?

Thanks.

Norman

    You can use regular expressions. However, if you are trying to play safe with HTML tags, you may want to consider [man]htmlspecialchars/man, [man]htmlentities/man, or [man]strip_tags/man.

      what is a HTML tag?
      it begins with '<'

      what is an email address?
      it has inside '@'

      so you can use http://php.net/strstr function
      it can search for 1 character or a substring/word,
      inside another string

      for example

      <?php
      
      $somestring = 'hello, my website is <a href="http://www.google.com">click</a>';
      $myemail = 'johnny_carson@hotmail.com';
      
      if( strstr( $somestring, '<' ))
          echo 'somestring has got HTML <TAG';
      else
          echo 'somestring without html tag';
      
      if( strstr( $myemail, '@hotmail.com' ))
          echo 'myemail is a hotmail email address';
      else
          echo 'myemail is not a correct hotmail';
      
        halojoy wrote:

        what is a HTML tag?
        it begins with '<'

        what is an email address?
        it has inside '@'

        Is this html?

        <book></book>

        Is this email?

        @go an play outside@
          • yes it looks like html
            that is, it is not a normal text string, it is someone trying to submit code
            with tags in a string
          • the second is not a valid email
            user will be asked to try again, and enter a valid email
            ----------
            but if you want to know more what those are:
            search with google
            for '<book>' and '@'

          because I never have needed to use the <book> tag myself

            halojoy wrote:

            - yes it looks like html
            that is, it is not a normal text string, it is someone trying to submit code
            with tags in a string
            - the second is not a valid email

            user will be asked to try again, and enter a valid email

            but if you want to know more what those are:
            search with google
            for '<book>' and '@'

            because I never have needed to use the <book> tag myself

            I just want to show u that your logic is flawed, because your script recognize this string:

            1<2

            as a html content ...
            I used book word in html tags format just to show u that is not a html, could have been a string with someone explaind how to create an xml file ...

            Anyway, wuno u should tell us why do u need that so we know how to help u ...

              perhaps using REGEX functions such as preg_match() would be a good choice then...

                why do people want to know if there are HTML tags?
                Because there are spams using <a href and injections and you name it

                if someone
                submits, in a form, which i think is the case for the asking person,
                1<2, he can be asked not to use '<'
                and if he wants to be clever and tell me 1<2

                he can say: 1 is less than 2

                my code in my first reply
                is a very simple way to determine, if is some HTML tags in a string

                But to be a bit more secure
                we might need to test for
                '&lt'
                which is same as '<'

                <?php
                
                if ( strstr( $string, '<' ) || strstr( $string, '&lt' ) )
                   exit( 'dont use HTML tags, please' );
                
                  halojoy wrote:

                  why do people want to know if there are HTML tags?
                  Because there are spams using <a href and injections and you name it

                  For this cases he could use htmlspecialchars(), htmlentities() or strip_tags() and he will have no need to check if the string contains html code ...

                    Write a Reply...