函数,它们的内容

函数,它们的内容

问题描述:

我有这个PHP代码一起删除HTML标签:函数,它们的内容

<?php 
    $mytext = '<b>1</b> hello world!'; 
    echo strip_tags($mytext); // result : 1 hello world! 
?> 

我想要去除html标签与内容,所以结果应该是这样的:

<?php 
    $mytext = '<b>1</b> hello world!'; 
    echo strip_tags_content($mytext); // result : hello world! 
?> 

我发现这function但我不知道这是否是安全的:

<?php 
function strip_tags_content($text, $tags = '', $invert = FALSE) { 

    preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags); 
    $tags = array_unique($tags[1]); 

    if(is_array($tags) AND count($tags) > 0) { 
    if($invert == FALSE) { 
     return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text); 
    } 
    else { 
     return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text); 
    } 
    } 
    elseif($invert == FALSE) { 
    return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text); 
    } 
    return $text; 
} 
?> 
+4

'result:1 hello world!'和'result:1 hello world!'有什么区别? – PeterMader

+0

安全吗?在这里定义安全。 – j08691

+0

注意:您的第一行的报价用法错误,您使用的是单引号,然后是双引号。 – Samuel

您如何运行你的代码...我不千牛你的第一条线是一团糟。 无论如何..实现你的结果你可以使用substr来实现你想要的输出。

$mytext = "<b>1</b> hello world!"; 
echo strip_tags($mytext);//1 hello world! 
$text = trim(substr(strstr($mytext, '</b>'), strlen('</b>'))); 
echo $text;//hello world!