Usage example of strip_tags() function in php

WBOY
Release: 2016-07-25 08:58:24
Original
1026 people have browsed it
This article introduces the usage of strip_tags() function in PHP. Friends in need can refer to it.

The strip_tags() function in php can strip HTML, XML and PHP tags.

Usage: strip_tags(string,allow) The following allow is optional. If filled in, it indicates what tags are allowed.

Attachment, php uses strip_tags to clear all tags.

string strip_tags(string str);

The function strip_tags can remove any HTML and PHP tag strings contained in the string. If the HTML and PHP tags of the string are originally wrong, for example, the greater than symbol is missing, an error will also be returned.

string strip_tags ( string str [, string allowable_tags] ) returns a string with html tags removed; you can use the second parameter to set tags that do not need to be deleted.

For example:

<?php
strip_tags($str, ""); 
//保留$str中的a标签
?>
Copy after login

Question 1, how does strip_tags retain multiple HTML tags?

Just separate multiple tags with spaces and write them to the second parameter of strip_tags, code:

strip_tags($str, "<p> <b>"); 
Copy after login

Question 2, How to delete specific tags in html tags in php?

Code:

<?php
/**
* 删除html标记中的特定标签
* edit bbs.it-home.org
*/
function strip_selected_tags($text, $tags = array())
{
        $args = func_get_args();
        $text = array_shift($args);
        $tags = func_num_args() > 2 ? array_diff($args,array($text)) : (array)$tags;
        foreach ($tags as $tag){
            if( preg_match_all( '/<'.$tag.'[^>]*>([^<]*)</'.$tag.'>/iu', $text, $found) ){
                $text = str_replace($found[0],$found[1],$text);
            }
        }

        return preg_replace( '/(<('.join('|',$tags).')( | |.)*/>)/iu', '', $text);
    }
$str = "[url="]123[/url]";
echo strip_selected_tags($str,array('b'));
?>
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template