开发者

Prevent or clear HTML/CSS tags

开发者 https://www.devze.com 2023-01-10 05:49 出处:网络
I\'m developing a website using PHP, MySQL and HTML. In the database, one of the fields is a text that may contain HTML tags such as <b> or <i>, for example.

I'm developing a website using PHP, MySQL and HTML. In the database, one of the fields is a text that may contain HTML tags such as <b> or <i>, for example.

My problem is that in a speci开发者_JAVA百科fic part of the website (a search section), I wanna display only a 'summary'/substring of this field.

The problem is: when I get a part of this field to display in the page, tags that are not closed influence in the way the rest of the page is displayed.

Two things would solve this problem:

  1. Prevent these specific tags to be shown;
  2. After the field is displayed, I want to close all 'open tags'.

Note that option number one would be much better.


You can use strip_tags before you display the summary to the user.


If the string xhtml compatible? If it is, you could try rolling your own function to match tags, and auto-append closing tags for them at the end.

Use regular expression to look for all opening and closing tags in the string, then loop through the tags. If it encounters an opening tag (without the "/"), then push it into a stack. If it encounters a closing, then pop the top of the stack.

When all the tags have been handled, what's left in the stack will need to be closed. Just pop them off one at a time, and append the closing to the back of your string.


I would do it this way::

function to get the string out of DB will return var $return

$tag = strip_tags($return); // will remove tags if exist

print '<p>'$tag'</p>;


its easy to use preg replace function

$search = array(


    "'<script[^>]*?>.*?</script>'si",   // strip out javascript
                    "'<[\/\!]*?[^<>]*?>'si",            // strip out html tags
                    "'([\r\n])[\s]+'",                  // strip out white space
                    "'&(quot|#34|#034|#x22);'i",        // replace html entities
                    "'&(amp|#38|#038|#x26);'i",         // added hexadecimal values
                    "'&(lt|#60|#060|#x3c);'i",
                    "'&(gt|#62|#062|#x3e);'i",
                    "'&(nbsp|#160|#xa0);'i",
                    "'&(iexcl|#161);'i",
                    "'&(cent|#162);'i",
                    "'&(pound|#163);'i",
                    "'&(copy|#169);'i",
                    "'&(reg|#174);'i",
                    "'&(deg|#176);'i",
                    "'&(#39|#039|#x27);'",
                    "'&(euro|#8364);'i",                // europe
                    "'&a(uml|UML);'",                   // german
                    "'&o(uml|UML);'",
                    "'&u(uml|UML);'",
                    "'&A(uml|UML);'",
                    "'&O(uml|UML);'",
                    "'&U(uml|UML);'",
                    "'&szlig;'i",

                    );
    $replace = array(   

                                                    "",
                        "",
                        "\\1",
                        "\"",
                        "&",
                        "<",
                        ">",
                        " ",
                        chr(161),
                        chr(162),
                        chr(163),
                        chr(169),
                        chr(174),
                        chr(176),
                        chr(39),
                        chr(128),
                        "ä",
                        "ö",
                        "ü",
                        "Ä",
                        "Ö",
                        "Ü",
                        "ß",

                    );

    $text = preg_replace($search,$replace,$yourtextasstring);
    echo $text;

use this and change $yourtextasstring to your string with will have html text or css

lets say code: $yourtextasstring = "text with html tags 1";

if you put this before preg replace it will show you result ="1" without tags

0

精彩评论

暂无评论...
验证码 换一张
取 消