2013-11-03 23 views
1

我只想從HTML字符串中刪除評論和空白,然後保存在數據庫中。我不希望它被修復,並添加頭標籤等PHP:如何使用整潔只是一個字符串,並沒有修復它

我花了幾個小時尋找這個,但找不到任何東西,誰能做到這一點告訴我,我需要什麼配置和哪個PHP整潔函數將只是「縮小」,而不是嘗試從html字符串中生成有效的html文檔?

+0

你到目前爲止試過的是什麼? –

+0

這裏的最佳答案是否有幫助? http://stackoverflow.com/questions/6225351/how-to-minify-php-page-html-output/6225382#6225382 – danielpsc

回答

0

下面的例子可以幫助你:

<?php 
function html2txt($document){ 
$search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript 
       '@<[\/\!]*?[^<>]*?>@si',   // Strip out HTML tags 
       '@<style[^>]*?>.*?</style>@siU', // Strip style tags properly 
       '@<![\s\S]*?--[ \t\n\r]*>@'   // Strip multi-line comments including CDATA 
); 
$text = preg_replace($search, '', $document); 
return $text; 
} 
?> 

你可以在http://php.net/manual/en/function.strip-tags.php

0

更多信息,你可以試試這個,

以下功能用於去除不需要的HTML註釋&的空白,

 function remove_html_comments_white_spaces($content = '') {  

        $content = preg_replace('~>\s+<~', '><', $content); 
        $content = preg_replace('/<!--(.|\s)*?-->/', '', $content); 

      return $content; 
     } 

即使喲你想刪除標籤,那麼你可以使用PHP內置函數strip_tags();

相關問題