【发布时间】:2014-01-29 20:18:11
【问题描述】:
这是我的 PHP 函数,用于从字符串输入中删除所有空 HTML 标记:
/**
* Remove the nested HTML empty tags from the string.
*
* @param $string String to remove tags
* @param null $replaceTo Replace empty string with
* @return mixed Cleaned string
*/
function crl_remove_empty_tags($string, $replaceTo = null)
{
// Return if string not given or empty
if (!is_string($string) || trim($string) == '') return $string;
// Recursive empty HTML tags
return preg_replace(
'/<(\w+)\b(?:\s+[\w\-.:]+(?:\s*=\s*(?:"[^"]*"|"[^"]*"|[\w\-.:]+))?)*\s*/?>\s*</\1\s*>/gixsm',
!is_string($replaceTo) ? '' : $replaceTo,
$string
);
}
我的正则表达式:/<(\w+)\b(?:\s+[\w\-.:]+(?:\s*=\s*(?:"[^"]*"|"[^"]*"|[\w\-.:]+))?)*\s*/?>\s*</\1\s*>/gixsm
我用http://gskinner.com/RegExr/ 和http://regexpal.com/ 对其进行了测试,效果很好。 但是当我尝试运行它时。服务器总是返回错误:
Warning: preg_replace(): Unknown modifier '\'
我不知道究竟是什么'\'出了问题。有人请帮帮我!
【问题讨论】:
-
很抱歉,我想删除不包含任何内容的 HTML 标签。这不是
strip_tags所做的。