2017-07-07 61 views
-1

我有這個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

回答

0

您如何運行你的代碼...我不千牛你的第一條線是一團糟。 無論如何..實現你的結果你可以使用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! 
相關問題