СообЧа > База Знаний > Интернет > Веб-строительство > PHP

Вопрос

Переменная $a содержит текст. Подскажите, как удалить из переменной все теги HTML.

Ответ

Я пользуюсь функцией, которая дана в официальном руководстве по РНР, и пока все ок, только поменяла ее в одном месте — помечено двумя звездочками:

function del_tags($page)
{
/* $page — лучше строка, но может передаваться и массив строк, return $page без тегов
*/

/*$page=file($addr);*/

$search = array ("'<script[^>]*?>.*?</script>'si", // Strip out javascript
"'<[\/\!]*?[^<>]*?>'si", // Strip out html tags
"'([\r\n])[\s]+'", // Strip out white space
"'&(quote|#34);'si", // Replace html entities
"'&(amp|#38);'si",
"'&(lt|#60);'si",
"'&(gt|#62);'si",
"'&(nbsp|#160);'si",
"'&(iexcl|#161);'si",
"'&(cent|#162);'si",
"'&(pound|#163);'si",
"'&(copy|#169);'si",
"'&#(\d+);'e"); // evaluate as php
$replace = array (" ", // **
" ", // **
"\\1",
" ",
"&",
"<",
">",
" ",
chr(161),
chr(162),
chr(163),
chr(169),
"chr(\\1)");

$page = preg_replace ($search, $replace, $page);
return $page;
}


Ирина Гришанова

$a=strip_tags($a);

strip_tags
(PHP 3>= 3.0.8, PHP 4 >= 4.0b2)

strip_tags — Strip HTML and PHP tags from a string Description

string strip_tags (string str [, string allowable_tags])

This function tries to return a string with all HTML and PHP tags stripped from a given str. It errors on the side of caution in case of incomplete or bogus tags. It uses the same tag stripping state machine as the fgetss() function.

You can use the optional second parameter to specify tags which should not be stripped.

Note: Allowable_tags was added in PHP 3.0.13, PHP4B3.



Copyright © 2000-2004 Сообщество Чайников
Контактная информация