2015-01-08 48 views
0

更確切地說,我需要能夠地帶HTML標籤一樣好這個腳本的作用:zubrag.com/tools/html-tags-stripper.php轉換HTML頁面,以純文本使用PHP

我需要能夠做到這一點在我的本地(XAMPP服務器)與任何URL,但現在我想用這個網址從剝離標籤,因爲這是凌亂它可以得到:http://static.anaf.ro/static/10/Timis/Timis.htm

我有,不起作用,我不知道爲什麼或如何解決它。 這裏的是代碼來自:nadeausoftware.com/articles/2007/09/php_tip_how_strip_html_tags_web_page

我已經添加了此行的代碼,但它仍然無法工作......

$text = file_get_contents('http://static.anaf.ro/static/10/Timis/Timis.htm'); 

下面是原始代碼(請注意,原代碼沒有行從上面被我添加的那行)

/** 
* Copyright (c) 2008, David R. Nadeau, NadeauSoftware.com. 
* All rights reserved. 
* See: 
* http://nadeausoftware.com/articles/2007/09/php_tip_how_strip_html_tags_web_page 
*/ 


$text = file_get_contents('http://static.anaf.ro/static/10/Timis/Timis.htm'); 

function strip_html_tags($text) 
{ 
    // PHP's strip_tags() function will remove tags, but it 
    // doesn't remove scripts, styles, and other unwanted 
    // invisible text between tags. Also, as a prelude to 
    // tokenizing the text, we need to insure that when 
    // block-level tags (such as <p> or <div>) are removed, 
    // neighboring words aren't joined. 
    $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', 

      // Add line breaks before & after blocks 
      '@<((br)|(hr))@iu', 
      '@</?((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(
      ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 
      "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", 
      "\n\$0", "\n\$0", 
     ), 
     $text); 

    // Remove all remaining tags and comments and return. 
    echo strip_tags($text); 
} 
+0

那麼,在哪裏調用'strip_html_tags($ your_text)'這是什麼結果呢?另外,你是否理解你向我們展示了一個函數定義?並沒有顯示函數調用? –

+0

我忘了提及我不知道PHP ..我只是一個新手。要轉換成純文本的html頁面將成爲http://static.anaf.ro/static/10/Timis/Timis.htm問題中包含的鏈接,除此之外,我不知道應該包含哪些內容code..this實際上是我希望得到的幫助...一些dirrections至少 – user656931

+1

然後,我建議你閱讀說明書 - http://php.net/manual/en/language.functions.php –

回答

0

它工作正常,但是從鏈接的正則表達式中的內容不工作。它不返回正確的字符集,那麼試試這個:

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', 
      // Add line breaks before and after blocks 
      '@</?((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(
      ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 
      "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", 
      "\n\$0", "\n\$0", 
     ), 
     $text); 
    return strip_tags($text); 
} 

/* Read an HTML file */ 
$raw_text = file_get_contents('http://static.anaf.ro/static/10/Timis/Timis.htm'); 

/* Get the file's character encoding from a <meta> tag */ 
preg_match("/<meta[^>]+charset=['\"]?(.*?)['\"]?[\/\s>]/i", $raw_text, $matches); 
$encoding = $matches[1]; 

/* Convert to UTF-8 before doing anything else */ 
$utf8_text = iconv($encoding, "utf-8", $raw_text); 

/* Strip HTML tags and invisible text */ 
$utf8_text = strip_html_tags($utf8_text); 

/* Decode HTML entities */ 
$utf8_text = html_entity_decode($utf8_text, ENT_QUOTES, "UTF-8"); 
echo $utf8_text; 

我改變什麼:

得到正確的字符集我僅僅用這個代替這個

/* Get the file's character encoding from a <meta> tag */ 
preg_match('@<meta\s+http-equiv="Content-Type"\s+content="([\w/]+)(;\s+charset=([^\s"]+))[email protected]', $raw_text, $matches); 
$encoding = $matches[3]; 

編號1: Gues來自網站的腳本在從您提供的URL中剝離標籤時遇到了一些問題。它顯示了很多的。我想剝離標籤的最好方法就是簡單地去掉開頭的<和第一個結束>之間的所有內容。但我目前沒有任何正則表達式的想法,也許谷歌可以幫助:)

+0

不錯修復...我們可以添加一些換行符來使內容可讀嗎?對不起,延遲迴復,我正在努力學習功能:) – user656931