我正在处理项目,在主页中我们显示用户详细信息,每个用户自己更新其详细信息,我们也允许详细使用 HTML 标签。
但我面临的问题是,如果有人添加一些 HTML 详细信息,例如
<p><span style="color:#4c4c4c;font-family:Verdana;">Graphic Designer. Contact ***** .</span></p>
但是在索引页面上,我们显示的细节很少,它像这样进入主页
<p><span style="color:#4c4c4c;font-family:Verda
这会干扰主页的所有结构。
我试过PHP函数
strip_tags
这对我不起作用,我还编写了自己的函数来去除主页上的这些标签
function strip_html_tags( $text )
{
$text = preg_replace(
array(
// Remove invisible content
'@<head[^>]*?>.*?</head>@siu',
'@<style[^>]*?>.*?</style>@siu',
'@<script[^>]*?.*?</script>@siu',
'@<object[^>]*?.*?</object>@siu',
'@<embed[^>]*?.*?</embed>@siu',
'@<applet[^>]*?.*?</applet>@siu',
'@<noframes[^>]*?.*?</noframes>@siu',
'@<noscript[^>]*?.*?</noscript>@siu',
'@<noembed[^>]*?.*?</noembed>@siu',
'@</?((address)|(blockquote)|(center)|(del))@iu',
'@</?((div)|(h[1-9])|(ins)|(isindex)|(p)|(pre))@iu',
'@</?((dir)|(dl)|(dt)|(dd)|(li)|(menu)|(ol)|(ul))@iu',
'@</?((table)|(th)|(td)|(caption))@iu',
'@</?((form)|(button)|(fieldset)|(legend)|(input))@iu',
'@</?((label)|(select)|(optgroup)|(option)|(textarea))@iu',
'@</?((frameset)|(frame)|(iframe))@iu',
),
array(
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',"$0", "$0", "$0", "$0", "$0", "$0","$0", "$0",), $text );
return strip_tags( $text);
}
这也行不通。
我需要的是:剥离所有HTML标签以在主页上正确显示。
谢谢
strip_tags()
内置于 PHP 中
HTML净化器是更强大的解决方案
您是否考虑过简单地使用 html_entities()
?这会将"<"替换为"<",将">"替换为">"等。它允许您将其回显到页面,而不必担心它会干扰页面。
你的个人信息不仅仅是保存为实体而不是纯文本吗?这可以解释为什么strip_tags不起作用。
在这种情况下,请使用:html_entity_decode显示实际的HTML