Inhaltsverzeichnis
PHP strip_tags保留多个HTML标签的方法,strip_tags标签
Heim php教程 php手册 PHP strip_tags保留多个HTML标签的方法,strip_tags标签

PHP strip_tags保留多个HTML标签的方法,strip_tags标签

Jun 12, 2016 pm 04:30 PM
strip

PHP strip_tags保留多个HTML标签的方法,strip_tags标签

本文介绍了PHP strip_tags函数保留多个HTML标签的方法,可以使用第二个参数来设置不需要删除的标签,主要涉及到strip_tags的第二个参数

strip_tags 函数

语法
string strip_tags ( string str [, string allowable_tags] )
返回一个去除了HTML标签的字符串;可以使用第二个参数来设置不需要删除的标签。

使用方法:

前提:假如现在有这样一个字符串,

复制代码 代码如下:
$str = "

我来自帮客之家

";

1,不保留任何HTML标签,代码会是这样:
复制代码 代码如下:
echo strip_tags($str);
// 输出:我来自帮客之家

2,只保留一个标签的话,只需要将字符串写到strip_tags的第二个参数中:
 
复制代码 代码如下:
echo strip_tags($str, "
");
// 输出:我来自
帮客之家

3,要保留

…多个标签,只需要将多个标签用空格分隔后写到strip_tags的第二个参数中:
 
复制代码 代码如下:
echo strip_tags($str, "

");
// 输出:

我来自帮客之家


如果要使用php删除html标记中的特定标签呢?

这个就需要代码来实现了,如下:

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'));
Nach dem Login kopieren

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
2 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Repo: Wie man Teamkollegen wiederbelebt
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Abenteuer: Wie man riesige Samen bekommt
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)